当前位置:网站首页>Leetcode 146. LRU 缓存
Leetcode 146. LRU 缓存
2022-07-04 07:41:00 【von Libniz】
题目描述
请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
实现 LRUCache 类:
LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存
int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
void put(int key, int value) 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。
函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。
输入
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]
解释
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {
1=1}
lRUCache.put(2, 2); // 缓存是 {
1=1, 2=2}
lRUCache.get(1); // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {
1=1, 3=3}
lRUCache.get(2); // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {
4=4, 3=3}
lRUCache.get(1); // 返回 -1 (未找到)
lRUCache.get(3); // 返回 3
解法一:哈希表+链表
这题和数据流之类的题目一样,都是一个套路:用链表来记录数据顺序,用哈希表快速找到链表节点,再根据需求移动节点位置。这里用一个单向链表来实现,哈希表存储key对应value节点的前一个节点。当然也可以直接用双向链表。
class ListNode:
def __init__(self, key, value, next=None):
self.key = key
self.value = value
self.next = next
class LRUCache:
"""解法1:HashMap + LinkedList"""
def __init__(self, capacity: int):
self.dummy = ListNode(-1, -1)
self.tail = self.dummy
self.key_to_prev = {
}
self.capacity = capacity
def get(self, key: int) -> int:
if key not in self.key_to_prev:
return -1
self.remove_to_last(key)
return self.key_to_prev.get(key).next.value
def put(self, key: int, value: int) -> None:
if key in self.key_to_prev:
self.remove_to_last(key)
self.tail.value = value
return
if len(self.key_to_prev) < self.capacity:
self.add_key_value(key, value)
return
# not enough capacity
self.remove_lru()
self.add_key_value(key, value)
def set_value(self, key, value):
pre_node = self.key_to_prev.get(key)
target_node = pre_node.next
target_node.value = value
def add_key_value(self, key, value):
new_node = ListNode(key, value)
self.key_to_prev[key] = self.tail # s
self.tail.next = new_node
self.tail = new_node
def remove_lru(self):
remove_target = self.dummy.next
self.key_to_prev.pop(remove_target.key)
self.dummy.next = remove_target.next
# capacity 可能为1,所以还是要判断target.next是否为空
if remove_target == self.tail:
self.tail = self.dummy
else:
self.key_to_prev[remove_target.next.key] = self.dummy
def remove_to_last(self, key):
pre = self.key_to_prev[key]
target = pre.next
if self.tail == target:
return
pre.next = target.next
self.key_to_prev[target.next.key] = pre
target.next = None
self.tail.next = target
self.key_to_prev[key] = self.tail
self.tail = target
解法二:OrderedDict
直接使用内置的数据结构:OrderedDict。
class LRUCache:
""" 解法2:OrderedDict """
def __init__(self, capacity: int):
self.cache = OrderedDict()
self.capacity = capacity
def get(self, key: int) -> int:
if key not in self.cache:
return -1
value = self.cache.pop(key) # 访问了, 先删除
self.cache[key] = value # 再加到最后
return value
def put(self, key: int, value: int) -> None:
if key in self.cache:
self.cache.pop(key) # 先删除
self.cache[key] = value # 再加到最后
if len(self.cache) > self.capacity:
self.cache.popitem(last=False)
边栏推荐
- Xcode 14之大变化详细介绍
- How to send mail with Jianmu Ci
- How to use MOS tube to realize the anti reverse connection circuit of power supply
- How to buy financial products in 2022?
- Zephyr 学习笔记1,threads
- When JDBC connects to es query, is there a God who meets the following situation?
- 2022-021ARTS:下半年开始
- rapidjson读写json文件
- 【FreeRTOS】FreeRTOS學習筆記(7)— 手寫FreeRTOS雙向鏈錶/源碼分析
- [Flink] temporal semantics and watermark
猜你喜欢

Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future

L2-013 red alarm (C language) and relevant knowledge of parallel search
![[real case] how to deal with the failure of message consumption?](/img/ec/2bb2f0ff53c586bf45f364210658d7.jpg)
[real case] how to deal with the failure of message consumption?
![[kubernetes series] kubesphere is installed on kubernetes](/img/2b/eb39cf78b3bb9908b01f279e2f9958.png)
[kubernetes series] kubesphere is installed on kubernetes

Oracle stored procedures and functions

Système de surveillance zabbix contenu de surveillance personnalisé

大厂技术专家:架构设计中常用的思维模型

Unity opens the explorer from the inspector interface, selects and records the file path

神经网络入门(下)

墨者学院-phpMyAdmin后台文件包含分析溯源
随机推荐
[Flink] temporal semantics and watermark
L1-030 one gang one (15 points)
With excellent strength, wangchain technology, together with IBM and Huawei, has entered the annual contribution list of "super ledger"!
Technical experts from large factories: common thinking models in architecture design
[freertos] freertos Learning notes (7) - written freertos bidirectionnel Link LIST / source analysis
MySQL 数据库 - 函数 约束 多表查询 事务
Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
window上用.bat文件启动项目
PCIE知识点-010:PCIE 热插拔资料从哪获取
Flask 常用组件
L1-021 important words three times (5 points)
Oracle-存储过程与函数
Used on windows Bat file startup project
PCIe knowledge points -010: where to get PCIe hot plug data
OKR vs. KPI figure out these two concepts at once!
Rapidjson reading and writing JSON files
JVM中堆概念
Jianmu continuous integration platform v2.2.2 release
[network security] what is emergency response? What indicators should you pay attention to in emergency response?