题目来源:

  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. .net 生成缩略图

    public static void CreateSmallImage(string minImageFullPath, System.Drawing.Image originalImage, int ...

  2. android MVC理解

    算来学习Android开发已有2年的历史了,在这2年的学习当中,基本掌握了Android的基础知识.越到后面的学习越感觉困难,一来是自认为android没啥可学的了(自认为的,其实还有很多知识科学), ...

  3. Java文件IO操作应该抛弃File拥抱Path和Files

    Java7中文件IO发生了很大的变化,专门引入了很多新的类: import java.nio.file.DirectoryStream;import java.nio.file.FileSystem; ...

  4. oracle group by 使用

    SELECT supplier_id, max(evidence_date) AS evidence_date,max(TD_SUPPLIER_EVIDENCE_INFO_ID) AS TD_SUPP ...

  5. 数组转DataTable

    using System; using System.Data; namespace ArrayToDataTable { class ArrayToDataTable { /// <summa ...

  6. css reset 以及哪些元素有默认margin padding值

    很多网站在css reset 的时候有如下做法: body, ul, ol, li, p, h1, h2, h3, h4, h5, h6, form, label, dl, dt, dd,fields ...

  7. python基础(三)

    set集合 set集合创建 #方式1: se = {'} #与字典类似dict1 = {'k1','v1','k2','v2'} #方式2: se = set() #创建一个空的集合 list1 = ...

  8. jQuery停止事件冒泡

    event.stopPropagation(); 在jQuery中提供了stopPropagation()方法来停止事件冒泡.终止事件在传播过程的捕获.目标处理或起泡阶段进一步传播.调用该方法后,该节 ...

  9. ListView的Item点击事件(消息传递)

    转载请保留原文出处“http://my.oschina.net/gluoyer/blog”,谢谢! 您可以到博客的“友情链接”中,“程序猿媛(最新下载)*.*”下载最新版本,持续更新!当前版本,也可直 ...

  10. LOL是什么意思? - 已解决 - 搜狗问问

    LOL是什么意思? - 已解决 - 搜狗问问 N A T S U . |分类:QQ工具栏 2009-05-04 LOL是什么意思? 满意答案 Shim Nyong 19级 2009-05-04 LOL ...