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. Linux多线程编程小结

     Linux多线程编程小结 前一段时间由于开题的事情一直耽搁了我搞Linux的进度,搞的我之前学的东西都遗忘了,非常烦躁的说,如今抽个时间把之前所学的做个小节.文章内容主要总结于<Linux程序 ...

  2. Mansory算法分析

    相信大家对mansory排版算法印象十分深刻,它能够十分有效的实现页面紧凑排版,节省空间,并且还显得十分美观.在很多网站,包括鼎鼎有名的pinterest都使用了这个算法来实现排版.这个过程有点象瓦匠 ...

  3. RCTF Welpwn

    Welpwn 很久以前的了,现在整理一下 题目的漏洞很明显,就是一个栈溢出.程序打开了NX,却没有给libc.重点是,在向栈上拷贝数据时,如果输入中含有'\x00',会被截断,对利用漏洞造成了困难.虽 ...

  4. 【递推】【HDU2585】【hotel】

    Hotel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  5. ckplayer网页播放器简易教程

    前言 ckplayer是一款在网页上播放视频的免费视频插件,该插件兼容性强.使用简单.api齐全.另外,任何个人网站或商业网站在不修改右键版权的基础上都可以免费使用. 下面将对ckplayer的整个使 ...

  6. C#l连接OPC进行数据交互

    步骤 :引用 OPCNETAPI.DLL&&OPCNETAPI.COM.DLL 1.查询服务器      2. 连接服务器  3. 读取数据     4.写入数据 1.查询服务器 :根 ...

  7. CSS3中轻松实现渐变效果

    background: -moz-linear-gradient(top, #8fa1ff, #3757fa); /* Firefox */ background: -webkit-gradient( ...

  8. c++对文件操作的支持(二)

    #include <stdio.h> #include <iostream> #include <fstream> using namespace std; voi ...

  9. Excel转JSON-简单-暴力-迅速

    一直在做一个关于网上选课的系统,选用了时下比较流行的node.js.今天在想怎么把学生或者老师的信息导入进去,涉及数量比较多一点,我手边又正好有一部分excel的表格.就想把excel转成json然后 ...

  10. 转发:使用sql命令查询视图中所有引用的基础表

    转自:使用sql命令查询视图中所有引用的基础表 使用sql命令查询视图中所有引用的基础表 之前有写过如何利用sql查询视图中所有引用的表发现这个方法并不能查出视图中所有的基础表,如果视图中有嵌套视图就 ...