【LeetCode】146. LRU緩存機制

題目:

運用你所掌握的數據結構,設計和實現一個 LRU (最近最少使用) 緩存機制。它應該支持以下操作: 獲取數據 get 和 寫入數據 put

獲取數據 get(key) - 如果密鑰 (key) 存在於緩存中,則獲取密鑰的值(總是正數),否則返回 -1。
寫入數據 put(key, value) - 如果密鑰不存在,則寫入其數據值。當緩存容量達到上限時,它應該在寫入新數據之前刪除最近最少使用的數據值,從而爲新的數據值留出空間。

進階:

你是否可以在 O(1) 時間複雜度內完成這兩種操作?

示例:

LRUCache cache = new LRUCache( 2 /* 緩存容量 */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // 返回  1
cache.put(3, 3);    // 該操作會使得密鑰 2 作廢
cache.get(2);       // 返回 -1 (未找到)
cache.put(4, 4);    // 該操作會使得密鑰 1 作廢
cache.get(1);       // 返回 -1 (未找到)
cache.get(3);       // 返回  3
cache.get(4);       // 返回  4
思路:

​ 由題目中要求的O(1)時間複雜度想到緩存可以想到用一個map來存儲key、value結點,題目最近最少使用到的(緩存)放到最後,最新訪問的(緩存)放到最前面,可以考慮用雙端隊列來實現,這樣,這個map的key對應的是緩存的Key, value對應的是雙端隊列的一個節點,即隊列的節點同時存在map的value中。

​ 這樣,當新插入一個節點時,它應該在這個雙端隊列的隊頭處,同時把這個節點的key和這個節點put到map中保留下來。當LRU緩存隊列容量達到最大又要插入新元素時,把隊列的最後一個節點刪除掉,同時在map中移除該節點對應的key。這個雙端隊列的數據結構爲:

class DLinkedList{
    int key;
    int value;
    DLinkedList pre; //前一個節點
    DLinkedList next;//後一個節點
}

​ 這個map就用HashMap即可。

解答代碼爲:
class DLinkedList{
    int key;
    int value;
    DLinkedList pre;
    DLinkedList next;
}

class LRUCache {  
    private DLinkedList head;
    private DLinkedList tail;
    private Map <Integer, DLinkedList> cache;
    private int count;
    private int capacity;
    
    public LRUCache(int capacity) {
        count = 0;
        this.capacity = capacity; 
        cache = new HashMap<Integer, DLinkedList>();
        head = new DLinkedList();
        tail = new DLinkedList();
        head.pre = null;
        head.next = tail;
        tail.next = null;
        tail.pre = head;
    }
    
    public void add(DLinkedList node){
        node.pre = head;
        node.next = head.next;
        head.next.pre = node;
        head.next = node;
    }
    
    public void remove(DLinkedList node){
        DLinkedList pre = node.pre;
        DLinkedList next = node.next;
        pre.next = next;
        next.pre = pre;
    }
    
    public void moveToHead(DLinkedList node){
        remove(node);
        add(node);
    }
    
    //刪除隊尾元素
    public DLinkedList popTail(){
        DLinkedList res = tail.pre;
        remove(res);
        return res;
    }
    
    public int get(int key) {
        DLinkedList node = cache.get(key);
        if(node == null){
            return -1;
        }
        moveToHead(node);
        return node.value;
    }
    
    public void put(int key, int value) {
        DLinkedList node = cache.get(key);
        if(node != null){
            node.value = value;
            moveToHead(node);
            return;
        } 
        //如果隊列數量大於容量,刪掉隊尾最近最少使用的那個元素
        if(++count > capacity){
           DLinkedList delNode = popTail();
           cache.remove(delNode.key);
        }
        node = new DLinkedList();
        node.key = key;
        node.value = value;
        add(node);
        cache.put(key,node);
    }
}

/**
 * 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);
 */
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章