import java.util.*; class LRUCache { private LinkedHashMap<Integer, Integer> cache; private int capacity; private final float factor = 0.75f; public LRUCache(int k) { int size = (int) Math.ceil(k / factor) + 1; this.cache = new LinkedHashMap<Integer, Integer>(size, 0.71f, true) { @Override protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) { return size() > capacity; } }; this.capacity = k; } public synchronized int size() { return this.cache.size(); } public void set(int key, int value) { this.cache.put(key, value); } public int get(int key) { Integer res = this.cache.get(key); if (res == null) { return -1; } return res; } } 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 // write code here LRUCache solution = new LRUCache(k); List<Integer> resList = new ArrayList<>(); for (int i = 0; i < operators.length; i++) { int[] line = operators[i]; if (line[0] == 1) { solution.set(line[1], line[2]); } else { int i1 = solution.get(line[1]); resList.add(i1); } } int[] res = new int[resList.size()]; for (int i = 0; i < res.length; i++) { res[i] = resList.get(i); } return res; } }