leetcode LRU Cache python
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的更多相关文章
- [LeetCode] LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LeetCode]LRU Cache有个问题,求大神解答【已解决】
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- LeetCode:LRU Cache
题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...
- LeetCode——LRU Cache
Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...
- LeetCode: LRU Cache [146]
[题目] Design and implement a data structure for Least Recently Used (LRU) cache. It should support th ...
- LeetCode – LRU Cache (Java)
Problem Design and implement a data structure for Least Recently Used (LRU) cache. It should support ...
- Leetcode: LRU Cache 解题报告
LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should supp ...
- [LeetCode] LRU Cache [Forward]
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- Leetcode:LRU Cache,LFU Cache
在Leetcode上遇到了两个有趣的题目,分别是利用LRU和LFU算法实现两个缓存.缓存支持和字典一样的get和put操作,且要求两个操作的时间复杂度均为O(1). 首先说一下如何在O(1)时间复杂度 ...
随机推荐
- 【递推】【HDU2585】【hotel】
Hotel Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 关于Console的Main(String[] args)参数输入
之前接触一个往Console里输入参数的项目,资深QA教我怎么run,灰常脸红. 今日无事,baidu之. Step1 写简单Console Code. class Program { static ...
- JavaScript 面向对象思想 贪吃蛇游戏
js代码: 游戏的对象 ,食物,蛇 ,游戏控制思路如下 (完整代码在https://github.com/774044859yf/ObjectSnakeGame下载) var snake = { aS ...
- python学习笔记:python数字
一.数字类型分类 数字提供了标量存储和直接访问,它是不可更改类型,也就是说变更数字的值会产生新的对象.python的对象模型与常规对象模型有些不同,对数字对象的更新,实际上是生成了一个新的数值对象,并 ...
- spring sts 从数据库中反向生成实体类
首先我们要在sts中建立mysql的数据库连接 1. 当点击ok之后,如果没有报错的话就应该是建立好了,我们可以点击查看这个数据库中所有的表 我们就可以再sts进行数据库操作了,具体如下: 点击如下按 ...
- hdu 5442 Favorite Donut 最大表示法+kmp
题目链接 给你一个字符串, 然后把他想象成一个环. 从某一个地方断开,然后逆时针或顺时针, 都可以形成一个字符串, 求字典序最大的那种. 输出断开位置以及是顺时针还是逆时针. 如果两个一样, 输出位置 ...
- Google机器学习教程心得(一)
Hello world Google Machine Learning Recipes 1 官方中文博客 http://chinagdg.org/2016/03/machine-learning-re ...
- 超轻量级高性能ORM数据访问组件Deft,比dapper快20%以上
超轻量级高性能ORM数据访问组件Deft,比dapper快20%以上 阅读目录 Deft简介 Deft 核心类介绍 Deft 3分钟即可上手使用 其他可选的配置参数 性能测试 Demo代码下载 回到顶 ...
- MYSQ 查看 2 进制日志
方法 1: myqlbinlog filename; ------------------------------------------------------------------------- ...
- Linux03--文件打包与解压
参考了<鸟哥的Linux私房菜> 1.压缩命令 gzip(压缩)与zcat(解压并读出来) gzip 可以说是应用度最广的压缩命令了!目前 gzip 可以解开 compress, zip ...