力扣 146. LRU 缓存
题目描述:
请你设计并实现一个满足 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) 的平均时间复杂度运行。
解析:
1.LinkedHashmap
2.哈希表和双端链表
Java:
class LRUCache {
int capacity;
LinkedHashMap<Integer, Integer> cache;
public LRUCache(int capacity) {
this.capacity = capacity;
cache = new LinkedHashMap<Integer, Integer>(capacity, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry eldest) {
return cache.size() > capacity;
}
};
}
public int get(int key) {
return cache.getOrDefault(key, -1);
}
public void put(int key, int value) {
cache.put(key, value);
}
}
class Node{//节点[pre|key|value|next]
int key,val;
Node next,pre;
public Node(int key,int val){
this.key = key;
this.val = val;
}
}
class DoubleList{//双链表head<->[key,val]<->tail
Node head;//头结点
Node tail;//尾结点
public DoubleList(){
head = new Node(0,0);
tail = new Node(0,0);
head.next = tail;
tail.pre = head;
}
void addFirst(Node n){//头插
head.next.pre = n;
n.next = head.next;
n.pre = head;
head.next = n;
}
void remove(Node n){//删除指定节点n
n.pre.next = n.next;
n.next.pre = n.pre;
}
Node removeLast(){//删除尾结点,并返回该节点
Node res = tail.pre;
remove(res);
return res;
}
}
class LRUCache {
HashMap<Integer,Node> map;
DoubleList cache;
int cap;//容量
public LRUCache(int capacity) {
map = new HashMap<>();
cache = new DoubleList();
this.cap = capacity;
}
public int get(int key) {
if(!map.containsKey(key))//若该节点不存在
return -1;
Node res = map.get(key);
cache.remove(res);
cache.addFirst(res);
return res.val;
}
public void put(int key, int value) {
Node n = new Node(key,value);
if(map.containsKey(key)){//若该节点已经存在
cache.remove(map.get(key));
}else if(map.size()==cap){//若该节点不存在,但是cache已满
Node last = cache.removeLast();
map.remove(last.key);
}
cache.addFirst(n);
map.put(key,n);
}
}
JavaScript:
class ListNode {
constructor(key, value) {//双向链表的单个节点
this.key = key
this.value = value
this.next = null //指向后一个节点
this.prev = null //指向前一个节点
}
}
class LRUCache {
constructor(capacity) {
this.capacity = capacity //容量
this.hashTable = {} //存放键值对信息
this.count = 0 //键值对数量
this.dummyHead = new ListNode() //dummy头节点 方便在链表从开始的地方插入
this.dummyTail = new ListNode() //dummy尾节点 方便在链表从末尾删除
this.dummyHead.next = this.dummyTail //dummyHead和dummyTail相互连接
this.dummyTail.prev = this.dummyHead
}
get(key) {
let node = this.hashTable[key]//查找哈希表中的键值对
if (node == null) return -1 //不存在该键值对 返回-1
this.moveToHead(node) //移动到链表头
return node.value
}
put(key, value) {
let node = this.hashTable[key] //哈希表中查找该键值对
if (node == null) {
let newNode = new ListNode(key, value) //不存在就创建节点
this.hashTable[key] = newNode //加入哈希表
this.addToHead(newNode) //加入链表头
this.count++ //节点数+1
if (this.count > this.capacity) { //超过容量 从队尾删除一个
this.removeLRUItem()
}
} else {
node.value = value //键值对存在于哈希表中 就更新
this.moveToHead(node) //移动到队头
}
}
moveToHead(node) {
this.removeFromList(node)//从链表中删除节点
this.addToHead(node)//将该节点添加到链表头
}
removeFromList(node) {//删除的指针操作
let tempForPrev = node.prev
let tempForNext = node.next
tempForPrev.next = tempForNext
tempForNext.prev = tempForPrev
}
addToHead(node) {//加入链表头的指针操作
node.prev = this.dummyHead
node.next = this.dummyHead.next
this.dummyHead.next.prev = node
this.dummyHead.next = node
}
removeLRUItem() {
let tail = this.popTail()//从链表中删除
delete this.hashTable[tail.key]//从哈希表中删除
this.count--
}
popTail() {
let tailItem = this.dummyTail.prev//通过dummyTail拿到最后一个节点 然后删除
this.removeFromList(tailItem)
return tailItem
}
}