[LeetCode]题解(python):146-LRU Cache
题目来源:
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的更多相关文章
- leetcode 146. LRU Cache 、460. LFU Cache
LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...
- 146. LRU Cache
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- 【LeetCode】146. LRU Cache 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+双向链表 日期 题目地址:https://le ...
- [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LeetCode] 146. LRU Cache 近期最少使用缓存
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- Java for LeetCode 146 LRU Cache 【HARD】
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- leetcode@ [146] LRU Cache (TreeMap)
https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...
- leetcode 146. LRU Cache ----- java
esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...
- 【LeetCode】146. LRU Cache
LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should suppo ...
- 146. LRU Cache (List, HashTable)
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
随机推荐
- Ajax实现动态的二级级联菜单
今天花了点时间用Ajax实现了一个二级级联菜单.整理总结一下.为了把重点放在Ajax和级联菜单的实现上,本文省略了数据库建表语句和操作数据库的代码! 数据库建表语句就不帖出来了.主要有两张表,区域表: ...
- C# for循环①护栏长度 ②广场砖面积 ③判断闰年平年
// static void Main(string[] args) { const double PI = 3.14; const int BAR_U ...
- 整数转字符与字符转整数的C系统函数
atoi (表示 alphanumeric to integer)是把字符串转换成整型数的一个函数 http://baike.baidu.com/link?url=VTP54JT5-EY5TL0GFf ...
- nginx解决502错误
添加一个域名绑定,突然服务器就挂了.由于是接受项目,没看过服务器配置.结果nginx和mysql 都跑不起来. 平滑启动nginx失败. 检查/usr/local/nginx/sbin/nginx - ...
- div中嵌套div速度将会同样很慢
---恢复内容开始--- div中嵌套了div速度将会同样很慢 最近很多老板在我们公司做企业站的时候都会要求说:我要div+css的,不要表格建的那种,那样不利于优化.但我们发现就算给他们用div ...
- php正则提取img所有属性值
$ext = 'gif|jpg|jpeg|bmp|png';//罗列图片后缀从而实现多扩展名匹配 by http://www.k686.com 绿色软件 $str = ''; $list = arra ...
- [Backbone.js]如何用backbone写一个仿网页版微信的webapp?
var Chat = Backbone.Model.extend({ idAttribute:'id', initialize:function(options){ var users = this. ...
- Delphi RichEdit的内容保存为图片
uses RichEdit; {将RichEdit1的内容保存为图片,此函数也适合于RxRichEdit,即RichEdit: TRxRichEdit}procedure RichEditToCanv ...
- 【翻译】Microsoft Ajax Minifier 快速使用指南(与VS集成使用) 编译后直接压缩项目的JS或CSS文件
网上找了好久终于找到一个能跟VS集成使用的JS和CSS压缩工具,因为害怕忘记,所以给转发过来,顺便翻译一下,大学那会儿学的英语基本上都已经还给老师了,所以翻译的不太好,不过能看懂就成,对吧? 原文地址 ...
- Javascript 排序数组或对象
分享一个用于数组或者对象的排序的函数.该函数可以以任意深度的数组或者对象的值作为排序基数对数组或的元素进行排序. 代码如下: /** * 排序数组或者对象 * by Jinko * date 2015 ...