1. 程式人生 > 其它 >LeetCode146-LRU快取機制

LeetCode146-LRU快取機制

得寫五遍才能記住

題目:

運用你所掌握的資料結構,設計和實現一個 LRU (最近最少使用) 快取機制 。

實現 LRUCache 類:
LRUCache(int capacity) 以正整數作為容量 capacity 初始化 LRU 快取
int get(int key) 如果關鍵字 key 存在於快取中,則返回關鍵字的值,否則返回 -1 。
void put(int key, int value): 如果關鍵字已經存在,則變更其資料值;
如果關鍵字不存在,則插入該組「關鍵字-值」。
當快取容量達到上限時,它應該在寫入新資料之前刪除最久未使用的資料值,從而為新的資料值留出空間。

連結:https://leetcode-cn.com/problems/lru-cache

解法1:

class LRUCache {

    private int cacheSize;
    private LinkedHashMap<Integer,Integer> map;

    public LRUCache(int capacity) {
        this.cacheSize = capacity;
        map = new LinkedHashMap<>(cacheSize,0.75F,true){
    
        @Override 
        protected boolean removeEldestEntry(Map.Entry<Integer,Integer> eldest){
            return size() > cacheSize;
        }
    };
   }
    
    public int get(int key) {
        return map.getOrDefault(key,-1);
    }
    
    public void put(int key, int value) {
        map.put(key,value);
    }
}

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache obj = new LRUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */

解法2

class LRUCache {

    private HashMap<Integer, Node> map;
    private int cacheSize;
    private LinkedNodeList cache;

    public LRUCache(int capacity) {
        this.cacheSize = capacity;
        map = new HashMap<>();
        cache = new LinkedNodeList();
    }

    public int get(int key) {
        if (!map.containsKey(key))
            return -1;
        Node res = map.get(key);
        cache.remove(res);
        cache.addFirst(res);
        return res.value;
    }

    public void put(int key, int value) {
        Node newnode = new Node(key, value);
        if (map.containsKey(key)) {
            cache.remove(map.get(key));
            cache.addFirst(newnode);
            map.put(key, newnode);
        } else {
            if (cacheSize == map.size()) {
                Node lastnode = cache.removeLast();
                map.remove(lastnode.key);
            }
            cache.addFirst(newnode);
            map.put(key, newnode);
        }
    }
}

class Node {
    int key;
    int value;
    Node prev;
    Node next;
    public Node() {
    }
    public Node(int k, int v) {
        this.key = k;
        this.value = v;
    }
}
class LinkedNodeList {
    Node head;
    Node tail;
    public LinkedNodeList() {
        head = new Node(0, 0);
        tail = new Node(0, 0);
        head.next = tail;
        tail.prev = head;
    }
    public void addFirst(Node n) {
        head.next.prev = n;
        n.next = head.next;
        n.prev = head;
        head.next = n;
    }
    public void remove(Node n) {
        n.prev.next = n.next;
        n.next.prev = n.prev;
    }
    public Node removeLast() {
        Node res = tail.prev;
        remove(res);
        return res;
    }
}



/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache obj = new LRUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */