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. java命令行运行main时jar及其配置

    run.bat中的内容如: set mypath=%cd%/../set classpath=%mypath%\conf;%mypath%\lib\*start /b java -Xms64m -Xm ...

  2. XPath在asp.net中查询XML

    .NET Framework 2.0中可以使用System.Xml.XPath命名空间下的类对XML文档进行基于路径的查询,在查询过程中需要构造类似SQL的查询字符串,该字符串遵循XPath语法.它由 ...

  3. 蜗牛爱课 -- iOS 设计模式之模板模式

    1 前言 模板方法模式是面向对象软件设计中一种非常简单的设计模式.其基本思想是在抽象类的一个方法定义“标准”算法.在这个方法中调用的基本操作由子类重载予以实现.这个方法成为“模板”.因为方法定义的算法 ...

  4. iOS设计模式解析(四)组合模式

    组合模式:将对象组合成树形结构以表示"部分-整体"的层次结构.组合是的用户对单个对象和组合对象的使用具有一致 Cocoa Touch中组合模式使用:Cocoa Touch框架中,U ...

  5. web.config详解

    在开发中经常会遇到这样的情况,在部署程序时为了保密起见并不将源代码随项目一同发布,而我们开发时的环境与部署环境可能不一致(比如数据库不一样),如果在代码中保存这些配置这些信息部署时需要到用户那里更改代 ...

  6. centos yum 完全卸载依赖

    centos yum 完全卸载依赖    you install a package with yum install, say pdftk, it will pull in a lot of dep ...

  7. 【linux信号】10.11信号集

    POSIX定义数据类型sigset_t以包含一个信号集,并且定义了下面五个函数处理信号集:

  8. Eclipse开启Mybatis-config.xml配置文件智能提示

    使用Java开发的程序员一般在学习的时候,可能都会涉及到使用配置文件,在使用Eclipse类似IDE进行编辑配置配置文件的时候,如果配置文件不能自动联想,是一件比较恼人的事情.笔者这里拿mybatis ...

  9. php设计模式笔记--总结篇

    一.引入  设计模式的一般定义不再说,只大概说一下我理解的设计模式,我理解的设计模式的主要目的是利用面向对象(类.接口等)特点,让代码更加易于扩展,易于重用,易于维护.这三个特点也就要求我们不要将太多 ...

  10. Array 的五种迭代方法 -----every() /filter() /forEach() /map() /some()

    ES5定义了五个迭代方法,每个方法都接收两个参数:要在每一项上运行的函数和运行该函数的作用域对象(可选的),作用域对象将影响this的值.传入这些方法中的函数会接收三个参数:数组的项的值.该项在数组中 ...