题目来源:

  https://leetcode.com/problems/lru-cache/


实现一个LRU缓存。直接上代码。


代码(python):

 class LRUCache(object):

     def __init__(self, capacity):
"""
:type capacity: int
"""
LRUCache.capacity = capacity
LRUCache.length = 0
LRUCache.dict = collections.OrderedDict() def get(self, key):
"""
:rtype: int
"""
try:
value = LRUCache.dict[key]
del LRUCache.dict[key]
LRUCache.dict[key] = value
return value
except:
return -1 def set(self, key, value):
"""
:type key: int
:type value: int
:rtype: nothing
"""
try:
del LRUCache.dict[key]
LRUCache.dict[key] = value
except:
if LRUCache.length == LRUCache.capacity:
LRUCache.dict.popitem(last = False)
LRUCache.length -= 1
LRUCache.dict[key] = value
LRUCache.length += 1

[LeetCode]题解(python):146-LRU Cache的更多相关文章

  1. leetcode 146. LRU Cache 、460. LFU Cache

    LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...

  2. 146. LRU Cache

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  3. 【LeetCode】146. LRU Cache 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+双向链表 日期 题目地址:https://le ...

  4. [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  5. [LeetCode] 146. LRU Cache 近期最少使用缓存

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  6. Java for LeetCode 146 LRU Cache 【HARD】

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  7. leetcode@ [146] LRU Cache (TreeMap)

    https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...

  8. leetcode 146. LRU Cache ----- java

    esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...

  9. 【LeetCode】146. LRU Cache

    LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should suppo ...

  10. 146. LRU Cache (List, HashTable)

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

随机推荐

  1. powerdesigner 转换各种数据库SQL

    转各种SQL脚本的步骤 一.

  2. Android ImageView图片自适应

    网络上下载下来的图片自适应:android:adjustViewBounds="true"(其详细解释在下面) <ImageView     android:id=" ...

  3. zoj1183 Scheduling Lectures

    这道题题意不想说了,跑了640ms,感觉水过去了,应该能通过单调队列优化,很长时间没碰已经不知道怎么写了,就说说现在的写法吧. 状态定义很关键:dp[i][j]把前j个topic放在前i堂课. 因为这 ...

  4. Android 弹出窗体

    findViewById(R.id.btn).setOnClickListener(new OnClickListener() { @Override public void onClick(View ...

  5. leetcode算法刷题(五)——动态规划(三)

    今天的题目不是leetcode上面的.只是觉得动态规划还是不算很熟练,就接着找了点DP的题练练 最长递增子序列的长度 题目的意思:传入一个数组,要求出它的最长递增子序列的长度.例如:如在序列1,-1, ...

  6. Oracle EBS-SQL (WIP-6):检查任务已完成但状态是发放的任务.sql

    select WE.WIP_ENTITY_NAME         ,MSI.SEGMENT1         ,MSI.DESCRIPTION         ,WDJ.CLASS_CODE     ...

  7. 欧几里得求最大公约数--JAVA递归实现

    欧几里得算法求最大公约数算法思想: 求p和q的最大公约数,如果q=0,最大公约数就是p:否则,p除以q余数为r,p和q的最大公约数即q和r的最大公约数. java实现代码: public class ...

  8. RoHS认证简介

    RoHS认证是<电气.电子设备中限制使用某些有害物质指令>(The restriction of the use of certain hazardous substances in el ...

  9. OS X 键盘快捷键

    了解有关常见 OS X 键盘快捷键的信息.键盘快捷键是通过按下键盘上的组合键来调用 OS X 功能的一种方式. 若要使用键盘快捷键或按键组合,您可以同时按修饰键和字符键.例如,同时按下 Command ...

  10. WIN ERROR:C:\Windows\System32\<LANG_NAME>\mstsc.exe.MUI

    Issue: When you upgrade Win7, you may found your remote desktop will not work. You may get following ...