【LeetCode 面试经典150题】146. LRU Cache LRU缓存

本文介绍了如何设计和实现一个遵循LRU策略的缓存数据结构,使用双向链表和哈希表相结合的方式,确保get和put操作的平均时间复杂度为O(1),并提供了相关示例和解题思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

146. LRU Cache

题目大意

Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.

Implement the LRUCache class:

  • LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
  • int get(int key) Return the value of the key if the key exists, otherwise return -1.
  • void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.

The functions get and put must each run in O(1) average time complexity.

中文释义

设计一个遵循最近最少使用(LRU)缓存约束的数据结构。

实现 LRUCache 类:

  • LRUCache(int capacity) 用正整数容量初始化 LRU 缓存。
  • int get(int key) 如果键存在,则返回键的值;否则,返回 -1。
  • void put(int key, int value) 如果键存在,更新其值。否则,将键值对添加到缓存中。如果此操作导致键的数量超出容量,那么淘汰最近最少使用的键。

getput 函数必须都具有 O(1) 的平均时间复杂度。

示例

示例 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); // LRU键是2,淘汰键2,缓存是 {1=1, 3=3}
lRUCache.get(2); // 返回 -1 (未找到)
lRUCache.put(4, 4); // LRU键是1,淘汰键1,缓存是 {4=4, 3=3}
lRUCache.get(1); // 返回 -1 (未找到)
lRUCache.get(3); // 返回 3
lRUCache.get(4); // 返回 4

限制条件

  • 容量范围为 1 <= capacity <= 3000
  • 键的范围为 0 <= key <= 104
  • 值的范围为 0 <= value <= 105
  • 最多将对 getput 调用 2 * 10^5 次。

解题思路

方法

该方案使用双数据结构:一个双向链表ÿ

### LeetCode 146 LRU Cache 的 C++ 实现 LRU(Least Recently Used)是一种常见的缓存淘汰策略,用于管理固定大小的内存空间。当缓存满时,会移除最近最少使用的数据项以腾出空间。 以下是基于双向链表和哈希表实现的 C++ 解决方案: #### 双向链表节点定义 为了高效地维护访问顺序并快速更新节点位置,可以使用自定义的 `ListNode` 类来表示双向链表中的节点。 ```cpp struct ListNode { int key; int value; ListNode* prev; ListNode* next; ListNode(int k, int v) : key(k), value(v), prev(nullptr), next(nullptr) {} }; ``` #### 缓存类设计 通过组合哈希表和双向链表,可以在 O(1) 时间复杂度下完成插入、删除以及查找操作。 ```cpp class LRUCache { private: unordered_map<int, ListNode*> map; // 哈希表存储键到节点指针的映射关系 ListNode* head; // 虚拟头结点 ListNode* tail; // 虚拟尾结点 int capacity; // 容量上限 public: LRUCache(int cap) : capacity(cap) { head = new ListNode(-1, -1); // 初始化虚拟头部 tail = new ListNode(-1, -1); // 初始化虚拟尾部 head->next = tail; // 连接首尾 tail->prev = head; } ~LRUCache() { ListNode* cur = head; while (cur != nullptr) { ListNode* temp = cur; cur = cur->next; delete temp; } } void removeNode(ListNode* node) { node->prev->next = node->next; node->next->prev = node->prev; } void addToHead(ListNode* node) { node->next = head->next; node->prev = head; head->next->prev = node; head->next = node; } int get(int key) { if (!map.count(key)) return -1; // 如果不存在该key,则返回-1 ListNode* node = map[key]; removeNode(node); addToHead(node); return node->value; } void put(int key, int value) { if (map.count(key)) { // 若已存在则更新其值并将它移到最前面 ListNode* node = map[key]; node->value = value; removeNode(node); addToHead(node); return; } if (map.size() >= capacity) { // 当容量达到上限时,移除最后未被使用的节点 ListNode* lastUsed = tail->prev; removeNode(lastUsed); map.erase(lastUsed->key); delete lastUsed; } ListNode* newNode = new ListNode(key, value); // 创建新节点并加入hashMap与链表前端 map[key] = newNode; addToHead(newNode); } }; ``` 上述代码实现了基本功能[^3],其中包含了以下几个核心部分: - **removeNode**: 将指定节点从当前列表中移除。 - **addToHead**: 把某个节点移动至链表开头的位置。 - **get 方法**: 获取对应 key 的值,并将其标记为最新访问过的项目。 - **put 方法**: 插入新的键值对或者覆盖已有条目;如果超出设定的最大数量限制,则清除掉最早之前加载的数据记录。 此版本的时间效率较高,在每次调用 `get()` 或者 `set()` 函数时都能保持常数级时间性能表现[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值