146. LRU 缓存
小于 1 分钟
146. LRU 缓存中等
class LRUCache {
private LinkedHashMap<Integer, Integer> cache;
public LRUCache(final int capacity) {
cache = new LinkedHashMap<Integer, Integer>(capacity, 0.75f, true) {
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return size() > capacity;
}
};
}
public int get(int key) {
return cache.getOrDefault(key, -1);
}
public void put(int key, int value) {
cache.put(key, value);
}
}
Powered by Waline v2.15.5