中文题目
运用所掌握的数据结构,设计和实现一个 LRU (Least Recently Used,最近最少使用) 缓存机制 。
实现 LRUCache
类:
LRUCache(int capacity)
以正整数作为容量capacity
初始化 LRU 缓存int get(int key)
如果关键字key
存在于缓存中,则返回关键字的值,否则返回-1
。void put(int key, int value)
如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。
示例:
输入 ["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
0 <= value <= 105
- 最多调用
2 * 105
次get
和put
进阶:是否可以在 O(1)
时间复杂度内完成这两种操作?
注意:本题与主站 146 题相同:https://leetcode-cn.com/problems/lru-cache/
通过代码
高赞题解
解题思路
可以把最新访问的节点放在链表头部或尾部,对应头插法和尾插法
代码
class LRUCache {
//头插法
class Node{
Node pre, next;
int key, val;
public Node(){
}
public Node(int key, int val){
this.key = key;
this.val = val;
}
}
Node head = new Node();
Node tail = new Node();
int size = 0;
Map<Integer, Node> map;
public void deleteNode(Node node){
node.next.pre = node.pre;
node.pre.next = node.next;
}
public void moveToHead(Node node){
deleteNode(node);
addToHead(node);
}
public void addToHead(Node node){
Node after = head.next;
head.next = node;
node.next = after;
after.pre = node;
node.pre = head;
}
public LRUCache(int capacity) {
size = capacity;
map = new HashMap<>();
head.next = tail;
tail.pre = head;
}
public int get(int key) {
if(!map.containsKey(key)) return -1;
Node node = map.get(key);
moveToHead(node);
return node.val;
}
public void put(int key, int value) {
if(map.containsKey(key)){
Node node = map.get(key);
node.val = value;
map.put(key, node);
moveToHead(node);
}else{
if(map.size() == size){
Node node = tail.pre;
deleteNode(node);
map.remove(node.key);
}
Node node = new Node(key, value);
map.put(key, node);
addToHead(node);
}
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/
class LRUCache {
//尾插法
class Node{
Node pre;
Node next;
int key;
int value;
public Node(int key, int value){
this.key = key;
this.value = value;
}
}
public void deleteNode(Node node){
node.next.pre = node.pre;
node.pre.next = node.next;
}
public void addToTail(Node node){
node.next = tail;
tail.pre.next = node;
node.pre = tail.pre;
tail.pre = node;
}
public void moveToTail(Node node){
deleteNode(node);
addToTail(node);
}
Map<Integer,Node> map;
int size;
Node head;
Node tail;
public LRUCache(int capacity) {
map = new HashMap<>();
size = capacity;
head = new Node(0,0);
tail = new Node(0,0);
head.next = tail;
tail.pre = head;
}
public int get(int key) {
if(!map.containsKey(key)) return -1;
Node node = map.get(key);
moveToTail(node);
return node.value;
}
public void put(int key, int value) {
if(map.containsKey(key)){
Node node = map.get(key);
node.value = value;
moveToTail(node);
}else{
if(map.size() == size){
Node prev = head.next;
deleteNode(prev);
map.remove(prev.key);
}
Node node = new Node(key, value);
addToTail(node);
map.put(key, node);
}
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
4019 | 7351 | 54.7% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|