一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第11天,点击查看活动详情。
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the LRUCache class:
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 get and put must each run in O(1) average time complexity.
Example 1:
Input
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
Output
[null, null, null, 1, null, -1, null, -1, 3, 4]
Explanation
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // cache is {1=1}
lRUCache.put(2, 2); // cache is {1=1, 2=2}
lRUCache.get(1); // return 1
lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
lRUCache.get(2); // returns -1 (not found)
lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
lRUCache.get(1); // return -1 (not found)
lRUCache.get(3); // return 3
lRUCache.get(4); // return 4
Constraints:
1 <= capacity <= 30000 <= key <= 1040 <= value <= 105- At most 2
* 105calls will be made togetandput.
The functions get and put must each run in O(1) average time complexity.
Map和链表组成。
/*
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制 。
实现 LRUCache 类:
LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存
int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。
示例:
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
*/
package com.linkedlist;
/**
* @Author you guess
* @Date 2022/4/5 21:59
* @Version 1.0
* @Desc 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);
*/
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
class NodePair {
Integer key;
Integer value;
public NodePair(int key, int value) {
this.key = key;
this.value = value;
}
}
class LRUCache {
int capacity;
private Map<Integer, NodePair> map;//作用是put、get的 T = O(1)
private LinkedList<NodePair> linkedList;//作用是1把put、get的节点放在开头,2删除节点
public LRUCache(int capacity) {
this.capacity = capacity;
this.map = new HashMap<>();
this.linkedList = new LinkedList<>();
}
public int get(int key) {
NodePair nodePair = map.get(key);
if (nodePair == null) {
return -1;
}
linkedList.remove(nodePair);//Removes the first occurrence of the specified element from this list, if it is present.
linkedList.addFirst(nodePair);//Inserts the specified element at the beginning of this list.
return nodePair.value;
}
public synchronized void put(int key, int value) {
NodePair insertNodePair = new NodePair(key, value);
NodePair nodePair = map.get(key);
//存在
if (nodePair != null) {
linkedList.remove(nodePair);
linkedList.addFirst(insertNodePair);
map.put(key, insertNodePair);
}
//不存在且已满
else if (this.capacity == map.size()) {
NodePair last = linkedList.removeLast();//Removes and returns the last element from this list.
map.remove(last.key);
linkedList.addFirst(insertNodePair);
map.put(key, insertNodePair);
} else {
//不存在且未满
map.put(key, insertNodePair);
linkedList.addFirst(insertNodePair);
}
}//put
}
public class Leetcode_146_LRUCache {
public static void main(String[] args) {
LRUCache lruCache = new LRUCache(2);
lruCache.put(1, 1); // cache is {1=1}
lruCache.put(2, 2); // cache is {1=1, 2=2}
System.out.println(lruCache.get(1)); // return 1
lruCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
System.out.println(lruCache.get(2)); // returns -1 (not found)
lruCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
System.out.println(lruCache.get(1)); // return -1 (not found)
System.out.println(lruCache.get(3)); // return 3
System.out.println(lruCache.get(4)); // return 4
}
//1
//-1
//-1
//3
//4
}
end