class Node(object):
def __init__(self,k,x):
self.key=k
self.val=x
self.prev=None
self.next=None
class DoubleLinkedList(object):
def __init__(self):
self.tail=None
self.head=None
def isEmpty(self):
return not self.None
def removeLast(self):
self.remove(self.tail)
def remove(self,node):
if self.head == self.tail:
self.head,self.tail = None,None
return
if node == self.head:
node.next.prev=None
self.head=node.next
return
if node == self.tail:
node.prev.next=None
self.tail=node.prev
return
node.prev.next=node.next
node.next.prev=node.prev
def addFirst(self,node):
if not self.head:
self.head=self.tail=node
node.prev=node.next=None
return
node.next=self.head
self.head.prev=node
self.head=node
node.prev=None class LRUCache(object): def __init__(self, capacity):
"""
:type capacity: int
"""
self.capacity=capacity
self.size=0
self.p=dict()
self.cache=DoubleLinkedList() def get(self, key):
"""
:rtype: int
"""
if (key in self.p) and self.p[key]:
self.cache.remove(self.p[key])
self.cache.addFirst(self.p[key])
return self.p[key].val
else:
return -1 def set(self, key, value):
"""
:type key: int
:type value: int
:rtype: nothing
"""
if key in self.p:
self.cache.remove(self.p[key])
self.cache.addFirst(self.p[key])
self.p[key].val=value
else:
node=Node(key,value)
self.p[key]=node
self.cache.addFirst(node)
self.size+=1
if self.size > self.capacity:
self.size-=1
del self.p[self.cache.tail.key]
self.cache.removeLast()

@link https://github.com/Linzertorte/LeetCode-in-Python/blob/master/LRUCache.py

leetcode LRU Cache python的更多相关文章

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

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

  2. [LeetCode]LRU Cache有个问题,求大神解答【已解决】

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

  3. LeetCode:LRU Cache

    题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...

  4. LeetCode——LRU Cache

    Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...

  5. LeetCode: LRU Cache [146]

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

  6. LeetCode – LRU Cache (Java)

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

  7. Leetcode: LRU Cache 解题报告

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

  8. [LeetCode] LRU Cache [Forward]

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

  9. Leetcode:LRU Cache,LFU Cache

    在Leetcode上遇到了两个有趣的题目,分别是利用LRU和LFU算法实现两个缓存.缓存支持和字典一样的get和put操作,且要求两个操作的时间复杂度均为O(1). 首先说一下如何在O(1)时间复杂度 ...

随机推荐

  1. 解决Win7&Win8 64位下Source Insight提示未完整安装的问题

    网上的破解版的注册表文件都是针对32位系统的,所以在64位系统里运行根本无法破解.下面分别贴出这俩系统里的破解文件. 使用方法: 分别复制对应系统的内容,新建文本文档,将内容粘贴进去,重命名为.reg ...

  2. nyoj 523 亡命逃窜 【BFS】

    亡命逃窜 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 从前有个叫hck的骑士,为了救我们漂亮的公主,潜入魔王的老巢,够英雄吧.只是英雄不是这么好当的.这个可怜的娃 ...

  3. jvm莫名退出问题解决

    当jvm莫名退出,没有留下任何任何信息的时候,在centos的 /var/log/dmesg文件中,或许可以找到一些端倪

  4. 视频编解码学习之路(H264)

    学习视频编解码技术很难吗?视频编解码技术的未来是什么? 明了的说,无论是软件还是硬件设计,视频编解码技术有很多难点,都需要很长一段时间积累才行. 从一开始接触MPEG-2到最新的H.264标准,可算走 ...

  5. Ubuntu 13.10 下安装node

    1.首先更新Ubuntu在线包:sudo apt-get update && sudo apt-get dist-upgrade, 2.默认Ubuntu已经安装python的,具体版本 ...

  6. WindowsForm 记事本 对话框

    textbox:     属性:         text:文本         selectedtext:获取或设置选中文本         canundo:是否能够撤销     方法:       ...

  7. JS参数使用带参数的方法

    大家都知道,在JS之中,一个变量的生命周期不是以大括号为界限的,所以即使是使用在循环或判断中的变量,外部也可以使用.可如果我们在循环或变量中使用了方法,而且这个方法使用了循环中的变量,那么后面的代码是 ...

  8. yum mysql

    linux下使用yum安装mysql   1.安装 查看有没有安装过:           yum list installed mysql*           rpm -qa | grep mys ...

  9. js问题学习

    1.前言为了node.js做准备,js的基本功还是很重要的.所以正值1024程序员节的时候所以找了些题目,整理了一下知识点.这篇文章感觉代码太多,难免枯燥,所以文章最后留了个彩蛋给读者. 2.简单回调 ...

  10. sass颜色

    1只定义一次颜色 {优点:可以给变量赋予不同的值: {缺点:变量名称更改与变量值混乱: 2变浅加深 /*颜色函数*/ .warning-box { background-color:lighten($ ...