LRU缓存机制
题目描述:
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。
获取数据 get(key) - 如果关键字 (key) 存在于缓存中,则获取关键字的值(总是正数),否则返回 -1。
写入数据 put(key, value) - 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字/值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。
分析上面的操作过程,要让 put 和 get 方法的时间复杂度为 O(1),我们可以总结出 cache 这个数据结构必要的条件:查找快,插入快,删除快,有顺序之分。
因为显然 cache 必须有顺序之分,以区分最近使用的和久未使用的数据;而且我们要在 cache 中查找键是否已存在;如果容量满了要删除最后一个数据;每次访问还要把数据插入到队头。
那么,什么数据结构同时符合上述条件呢?哈希表查找快,但是数据无固定顺序;链表有顺序之分,插入删除快,但是查找慢。所以结合一下,形成一种新的数据结构:哈希链表。
LRU 缓存算法的核心数据结构就是哈希链表,双向链表和哈希表的结合体。这个数据结构长这样:
![](https://img.haomeiwen.com/i18349227/ffb44de35a02c496.png)
思想很简单,就是借助哈希表赋予了链表快速查找的特性嘛:可以快速查找某个 key 是否存在缓存(链表)中,同时可以快速删除、添加节点。
作者:labuladong
链接:https://leetcode-cn.com/problems/lru-cache/solution/lru-ce-lue-xiang-jie-he-shi-xian-by-labuladong/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
示例:
LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // 返回 1
cache.put(3, 3); // 该操作会使得关键字 2 作废
cache.get(2); // 返回 -1 (未找到)
cache.put(4, 4); // 该操作会使得关键字 1 作废
cache.get(1); // 返回 -1 (未找到)
cache.get(3); // 返回 3
cache.get(4); // 返回 4
Java代码:
class LRUCache {
private Map<Integer, Node> map;
private ListNode listNode;
private int capacity;
public LRUCache(int capacity) {
map = new HashMap<>();
listNode = new ListNode();
this.capacity = capacity;
}
public int get(int key) {
Node node = map.get(key);
if(node == null) return -1;
put(key, node.getValue());
return node.getValue();
}
public void put(int key, int value) {
Node existNode = map.get(key);
if(existNode == null) {
if(listNode.getSize() >= capacity) {
Node deleteNode = listNode.deleteFirst();
map.remove(deleteNode.getKey());
}
}
Node node = new Node(key, value);
map.put(key, node);
listNode.removeNode(existNode);
listNode.addEnd(node);
}
public class Node{
private int key;
private int value;
private Node pre;
private Node next;
public Node (int key, int value) {
this.key = key;
this.value = value;
}
public int getKey() {
return key;
}
public int getValue() {
return value;
}
}
public class ListNode{
private Node first;
private Node end;
private int size;
public ListNode() {
first = null;
end = null;
size = 0;
}
public Node deleteFirst() {
if(first == null) return null;
Node temp = first;
if(first == end) {
first = null;
end = null;
}else {
first = first.next;
first.pre = null;
}
size--;
return temp;
}
public void addEnd(Node node) {
if(end == null) {
first = node;
end = node;
}else {
end.next = node;
node.pre = end;
end = node;
}
size++;
}
public void removeNode(Node node) {
if(node == null) return;
if(node == first && node == end) {
first = null;
end = null;
}else if(node == first) {
first = first.next;
first.pre = null;
}else if(node == end) {
end = end.pre;;
end.next = null;
}else {
node.pre.next = node.next;
node.next.pre = node.pre;
}
size--;
}
public int getSize() {
return size;
}
}
}
/**
* 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);
*/