【LeetCode with Python】 LRU Cache
来源:互联网 发布:高薪网络销售招聘 编辑:程序博客网 时间:2024/05/01 22:22
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/lru-cache/
题目类型:数据结构
难度评价:★★
文本地址:http://blog.csdn.net/nerv3x3/article/details/2920168
原题页面:https://oj.leetcode.com/problems/lru-cache/
题目类型:数据结构
难度评价:★★
文本地址:http://blog.csdn.net/nerv3x3/article/details/2920168
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:get
andset
.
get(key)
- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)
- Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
实现一个LRU缓存。最近最少使用LRU表的定义就不多说了,需要实现的get方法是根据key取value的,因此显然需要一个map,在Python里叫关联数组的帮助。虽然在leetcode中这道题被标为Hard级别,但其实这只是一道工程性质的题目。
class LRUCache: class LRUNode: def __init__(self, key, value): self.prev = None self.next = None self.key = key self.value = value # @param capacity, an integer def __init__(self, capacity): self.map = { } self.list_head = LRUCache.LRUNode(-1, -1) self.list_tail = LRUCache.LRUNode(-1, -1) self.list_head.next = self.list_tail self.list_tail.prev = self.list_head self.capacity = capacity def remove_node(self, node): node.prev.next = node.next node.next.prev = node.prev def append_node(self, new_node): last_node = self.list_tail.prev last_node.next = new_node new_node.prev = last_node new_node.next = self.list_tail self.list_tail.prev = new_node # @return an integer def get(self, key): if self.map.has_key(key): cur = self.map.get(key) self.remove_node(cur) self.append_node(cur) return cur.value else: return -1 # @param key, an integer # @param value, an integer # @return nothing def set(self, key, value): if self.map.has_key(key): old_node = self.map.get(key) self.remove_node(old_node) old_node.value = value self.append_node(old_node) else: if len(self.map) >= self.capacity: del_node = self.list_head.next self.remove_node(del_node) del self.map[del_node.key] new_node = LRUCache.LRUNode(key, value) self.append_node(new_node) self.map[key] = new_node
- 【LeetCode with Python】 LRU Cache
- [leetcode]LRU Cache (python)
- [Leetcode]146. LRU Cache @python
- LeetCode LRU 缓存 LRU Cache
- LRU Cache | leetcode
- Leetcode: LRU Cache
- LeetCode:LRU Cache
- Leetcode: LRU Cache
- [LeetCode] LRU Cache
- leetcode之LRU Cache
- [LeetCode]LRU Cache
- [LeetCode] LRU Cache
- leetcode LRU Cache
- LeetCode题解:LRU Cache
- LeetCode | LRU Cache
- Leetcode: LRU Cache
- [LeetCode] LRU Cache
- LeetCode OJ:LRU Cache
- 用带有PTDD Partition Table Doctor的WinPE处理硬盘0磁道损坏问题
- 在ACCESS中,"!"及"."符号之差别为何?
- 用带有PTDD Partition Table Doctor的WinPE修复硬盘主引导记录
- -
- tomcat控制台输出信息时会卡住的原因
- 【LeetCode with Python】 LRU Cache
- QQ宠物打工学习对照表
- 通过示例看页面处理中的事件
- ACCESS集锦
- My csdn blog is setup today, welcome to be here.
- 【LeetCode with Python】 Permutations
- 【LeetCode with Python】 Remove Element
- NetAdvantage Infragistics WPF 2008 v1 *SERIALS*
- Eclipse plugin & RCP technology guide (1) - Introduction of Eclipse and Eclipse plugin