[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 ...
随机推荐
- C# 输入输出 常量变量
//输出 Console.WriteLine("这是一行文字"); 自动回车的. Console.Write("Hello world"); 不带 ...
- 用JQuery实现表格隔行变色和突出显示当前行
用JQuery实现表格隔行变色和突出显示当前行 上源码 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "htt ...
- Linux定时任务Crontab命令详解
linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另 外, 由于使用者自己也可以设置计划任务,所以, ...
- PHP不显示报错了怎么办~
php.ini里设置吧, 找到 display_errors, 设置成on, error_reporting = E_ALL
- TCP粘包拆包问题
阿π 专注于网络协议,系统底层,服务器软件 C++博客 | 首页 | 发新随笔 | 发新文章 | | | 管理 Socket粘包问题 这两天看csdn有一些关于socket粘包,socket缓冲区设置 ...
- Linux下安装Wireshark
Linux下安装Wireshark wireshark依赖于libpcap,所以如果系统中未安装libpcap,也要将其一并安装 一.下载源码 源码文件 wireshark-x.x.x.tar.gz ...
- tempo 2.0 学习记录
最近在做项目时使用了tempo,感觉还不错,但是发现网上对于tempo 2.0 的介绍比较少,我也是在GitHub才找到了比较完整的使用说明,我也简单记录一下自己的使用过程,重新学习一下tempo 2 ...
- Keil MDK下如何设置非零初始化变量
一些工控产品,当系统复位后(非上电复位),可能要求保持住复位前RAM中的数据,用来快速恢复现场,或者不至于因瞬间复位而重启现场设备.而keil mdk在默认情况下,任何形式的复位都会将RAM区的非初始 ...
- 迎接 Windows Azure 和 DNN 挑战,几分钟内快速构建网站!
编辑人员注释:本文章由高级商务策划师兼开发平台推广者 Neeti Gupta 撰写. 曾几何时,构建一个简单的网站需要耗费好几个月的时间.在过去,.NET 开发人员和设计社区的一些成员使用 DNN(以 ...
- Hadoop HA部署
因为公司旧系统的Hadoop版本是2.2,所以在部署新系统时使用了旧系统. 但是在部署ResourceManager auto failover时发现其他nodemanager总是向0.0.0.0请求 ...