public class Solution {
/**
* lru design
* @param operators int整型二维数组 the ops
* @param k int整型 the k
* @return int整型一维数组
*/
public int[] LRU (int[][] operators, int k) {
// write code here
ArrayList<Integer> list = new ArrayList<Integer>();
LRUCache cache = new LRUCache(k);
for(int[] op : operators){
if(op[0]==1){
cache.put(op[1],op[2]);
}else{
int val = cache.get(op[1]);
list.add(val);
}
}
int[] ans = new int[list.size()];
for(int i=0;i<list.size();i++){
ans[i] = list.get(i);
}
return ans;
}
}
class LRUCache extends LinkedHashMap<Integer,Integer>{
int capacity;
public LRUCache(int capacity){
super(capacity,0.75f,true);
this.capacity = capacity;
}
public int get(int key){
return super.getOrDefault(key,-1);
}
public void put(int key, int val){
super.put(key,val);
}