请你设计并实现一个满足 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)的平均时间复杂度运行。
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the
LRUCacheclass:
LRUCache(int capacity)Initialize the LRU cache with positive sizecapacity.int get(int key)Return the value of thekeyif the key exists, otherwise return-1.void put(int key, int value)Update the value of thekeyif thekeyexists. Otherwise, add thekey-valuepair to the cache. If the number of keys exceeds thecapacityfrom this operation, evict the least recently used key.The functions
getandputmust each run inO(1)average time complexity.
思路
实现
map集合的key为key,value为双链表的节点,节点存储key和value
class LRUCache {
Map<Integer,ListNode> map;
int capacity;
ListNode head;
ListNode tail;
public LRUCache(int capacity) {
this.map = new HashMap<>();
this.capacity = capacity;
head = new ListNode(-1, -1);
tail = new ListNode(-1, -1);
head.next = tail;
tail.prev = head;
}
public int get(int key) {
ListNode listNode = map.get(key);
if (listNode == null){
return -1;
}
moveToTail(listNode,listNode.value);
return listNode.value;
}
public void put(int key, int value) {
if (map.containsKey(key)){
moveToTail(map.get(key),value);
}else{
if (map.size() == capacity){
map.remove(head.next.key);
deleteNode(head.next);
}
ListNode newNode = new ListNode(key,value);
map.put(key,newNode);
insertTotail(newNode);
}
}
public void deleteNode(ListNode listNode){
listNode.prev.next = listNode.next;
listNode.next.prev = listNode.prev;
}
public void moveToTail(ListNode listNode, int newValue){
deleteNode(listNode);
listNode.value = newValue;
insertTotail(listNode);
}
public void insertTotail (ListNode listNode){
tail.prev.next = listNode;
listNode.prev = tail.prev;
listNode.next = tail;
tail.prev = listNode;
}
}
class ListNode{
public int key;
public int value;
public ListNode next;
public ListNode prev;
public ListNode(int k, int v){
key = k;
value = v;
}
}
/**
* 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);
*/