请你设计并实现一个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) 的平均时间复杂度运行
class LRUCache {
int count;
Map
map; public LRUCache(int capacity) {
map=new LinkedHashMap<>();
count=capacity;
}
public int get(int key) {
if(map.containsKey(key)){
int value=map.get(key);
map.remove(key);
map.put(key,value);
return map.get(key);
}else{
return -1;
}
}
public void put(int key, int value) {
if(map.containsKey(key)){
map.remove(key);
map.put(key,value);
return;
}
if(count>0){
map.put(key,value);
count--;
}else{
int head=map.keySet().iterator().next();
map.remove(head);
map.put(key,value);
}
}
}