LeetCode每日一題5月25日 LeetCode146.LRU緩存機制

問題描述:

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

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

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/lru-cache

示例:



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

解題思路:

本題首先要清楚什麼是LRU緩存:

LRU 的全稱是 Least Recently Used。也就是說我們認爲最近使用過的數據應該是是「有用的」,很久都沒用過的數據應該是無用的,內存滿了就優先刪那些很久沒用過的數據。

這題我是看了大神的解法寫的。

構建雙向鏈表和unordered_map

不過該雙向鏈表中有key,value。倆個值。

雙鏈表數據結構如下:

struct Node
{
    int key;
    int value;
    Node* pre;
    Node* next;
    // 構造函數初始化
    Node(int key, int value){
        this->key = key;
        this->value = value;
        pre = nullptr;
        next = nullptr;
    }
};

哈希表定義爲unordered_map<int,Node*> map。map中的key和map中的value,也就是Node節點中的key是相等的。

每次實現插入時,先查看map裏面是否存在該元素,如果存在該元素,則將該元素移至表頭

                             若不存在,且map空間小於額定緩存,直接將該元素插入表頭,

                                 不存在,將鏈表尾端元素移除,該元素插入表頭。

首先實現鏈表節點的插入表頭和移出操作,時間複雜度都是O(1)

   void remove(Node* cur){
        if(cur==head) head=cur->next;
        else if(cur == tail) tail=cur->pre;
        else{
            cur->pre->next = cur->next;
            cur->next->pre = cur->pre;
        }
    }
    void setHead(Node* cur){
        cur->next = head;
        if(head!=nullptr){
            head->pre = cur;
        }
        head =cur;
        if(tail==nullptr){
            tail = head;
        }
    }

下面是完整代碼,unordered_map<int,Node*> 的查找是基於hashmap的,因此其find函數的時間複雜度爲O(1)

// 總的思想就是 哈希雙向鏈表
struct Node
{
    int key;
    int value;
    Node* pre;
    Node* next;
    // 構造函數初始化
    Node(int key, int value){
        this->key = key;
        this->value = value;
        pre = nullptr;
        next = nullptr;
    }
};

class LRUCache {
private:
    int size;
    Node* head;
    Node* tail;
    unordered_map<int,Node*> m;
public:
    LRUCache(int capacity) {
       this->size = capacity;
       head = nullptr;
       tail = nullptr;
    }
    
    int get(int key) {
        if(m.count(key)){
            Node* cur = m[key];
            int value = cur->value;
            remove(cur);
            setHead(cur);
            return value;
        }
        return -1;
    }
    
    void put(int key, int value) {
        if(m.count(key)>0){
            Node* cur = m[key];
            cur->value = value;
            remove(cur);
            setHead(cur);
        }else{
            Node* node = new Node(key,value);
            if(m.size()>=size){
                unordered_map<int,Node*>::iterator it = m.find(tail->key);
                remove(tail);
                m.erase(it);
            }
            setHead(node);
            m[key] = node;
        }
    }
    void remove(Node* cur){
        if(cur==head) head=cur->next;
        else if(cur == tail) tail=cur->pre;
        else{
            cur->pre->next = cur->next;
            cur->next->pre = cur->pre;
        }
    }
    void setHead(Node* cur){
        cur->next = head;
        if(head!=nullptr){
            head->pre = cur;
        }
        head =cur;
        if(tail==nullptr){
            tail = head;
        }
    }
};

/**
 * 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);
 */

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章