跳至主要內容

146. LRU 缓存

T4mako算法哈希表链表双向链表小于 1 分钟

146. LRU 缓存

中等

解法open in new window

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