题目:LeetCode)
请你设计并实现一个满足 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
lRUCache.get(4); // 返回 4
复制代码
提示:
1 <= capacity <= 3000
0 <= key <= 10000
- <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn><mo><</mo><mo>=</mo><mi>v</mi><mi>a</mi><mi>l</mi><mi>u</mi><mi>e</mi><mo><</mo><mo>=</mo><mn>1</mn><msup><mn>0</mn><mn>5</mn></msup></mrow><annotation encoding="application/x-tex">0 <= value <= 10^5</annotation></semantics></math>0<=value<=105
- 最多调用 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>2</mn><mo>∗</mo><mn>1</mn><msup><mn>0</mn><mn>5</mn></msup></mrow><annotation encoding="application/x-tex">2 * 10^5</annotation></semantics></math>2∗105 次
get
和put
解题思路
用哈希表加双链表,哈希表用于快速查询,双链表表示先后顺序,头部是最少使用的,尾部是最新使用的。
哈希表的键即是缓存 的key,值为链表中的节点。每个节点保存有key和value。每次访问某个缓存 时(get/put)要把它移到链表尾部,以表示访问过。当超出限制时,则把链表头部移除掉。
代码实现
class LRUCache {
private Map < Integer, ListNode > cache;
private ListNode head;
private ListNode tail;
private int capacity;
public LRUCache(int capacity) {
this.capacity = capacity;
cache = new HashMap < > ();
head = new ListNode(-1, -1);
tail = new ListNode(-1, -1);
head.next = tail;
tail.prev = head;
}
public int get(int key) {
if (!cache.containsKey(key)) {
return -1;
}
ListNode node = cache.get(key);
removeNode(node);
appendTail(node);
return node.value;
}
public void put(int key, int value) {
if (cache.containsKey(key)) {
ListNode node = cache.get(key);
node.value = value;
removeNode(node);
appendTail(node);
return;
}
ListNode newNode = new ListNode(key, value);
appendTail(newNode);
cache.put(key, newNode);
if (cache.size() > capacity) {
ListNode node = head.next;
cache.remove(node.key);
removeNode(node);
}
}
private void removeNode(ListNode node) {
node.prev.next = node.next;
node.next.prev = node.prev;
node.next = null;
node.prev = null;
}
private void appendTail(ListNode node) {
node.next = tail;
node.prev = tail.prev;
tail.prev.next = node;
tail.prev = node;
}
private class ListNode {
int key;
int value;
ListNode next;
ListNode prev;
public ListNode(int key, int value) {
this.key = key;
this.value = value;
}
}
}
复制代码
复杂度分析
- 空间复杂度:<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>O</mi><mo stretchy="false">(</mo><mn>1</mn><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">O(1)</annotation></semantics></math>O(1)
- 时间复杂度:<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">O(n)</annotation></semantics></math>O(n)
网友评论