若opt=2,接下来一个整数key,表示get(key),若key未出现过或已被移除,则返回-1
对于每个opt=2,输出一个答案
[[1,1,1],[1,2,2],[1,3,2],[2,1],[1,4,4],[2,2]],3
[1,-1]
[1,1,1],第一个1表示opt=1,要set(1,1),即将(1,1)插入缓存,缓存是{"1"=1} [1,2,2],第一个1表示opt=1,要set(2,2),即将(2,2)插入缓存,缓存是{"1"=1,"2"=2} [1,3,2],第一个1表示opt=1,要set(3,2),即将(3,2)插入缓存,缓存是{"1"=1,"2"=2,"3"=2} [2,1],第一个2表示opt=2,要get(1),返回是[1],因为get(1)操作,缓存更新,缓存是{"2"=2,"3"=2,"1"=1} [1,4,4],第一个1表示opt=1,要set(4,4),即将(4,4)插入缓存,但是缓存已经达到最大容量3,移除最不经常使用的{"2"=2},插入{"4"=4},缓存是{"3"=2,"1"=1,"4"=4} [2,2],第一个2表示opt=2,要get(2),查找不到,返回是[1,-1]
[[1,1,1],[1,2,2],[2,1],[1,3,3],[2,2],[1,4,4],[2,1],[2,3],[2,4]],2
[1,-1,-1,3,4]
import java.util.*;
public class Solution {
/**
* lru design
* @param operators int整型二维数组 the ops
* @param k int整型 the k
* @return int整型一维数组
*/
static class Node{
int key,value;
Node prev,next;
public Node(int key,int value){
this.key = key;
this.value = value;
}
}
private Map<Integer,Node> map = new HashMap<>();
private Node head = new Node(-1,-1);
private Node tail = new Node(-1,-1);
private int k;
public int[] LRU (int[][] operators, int k) {
// write code here
this.k = k;
head.next = tail;
tail.prev = head;
int len = (int) Arrays.stream(operators).filter(x->x[0]==2).count();
int[] ans = new int[len];
int cnt = 0;
for(int i=0;i<operators.length;i++){
if(operators[i][0] == 1){
set(operators[i][1],operators[i][2]);
}else{
ans[cnt++] = get(operators[i][1]);
}
}
return ans;
}
private void set(int key,int value){
if(get(key) > -1){
map.get(key).value = value;
}else{
if(map.size() == k){
int rk = tail.prev.key;
tail.prev.prev.next = tail;
tail.prev = tail.prev.prev;
map.remove(rk);
}
Node node = new Node(key,value);
map.put(key,node);
removeToHead(node);
}
return;
}
private int get(int key){
if(map.containsKey(key)){
Node node = map.get(key);
node.next.prev = node.prev;
node.prev.next = node.next;
//removetohead
removeToHead(node);
return node.value;
}
return -1;
}
public void removeToHead(Node node){
node.next = head.next;
head.next.prev = node;
head.next = node;
node.prev = head;
}
}
原文:https://www.cnblogs.com/nuli/p/15135348.html