[leetcode_146]LRU Cache

双链表模拟队列,然后两个map存储key-value和key-_ListNode优化查找时间。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
struct ListNodel {
    int key;
    ListNodel *before;
    ListNodel *next;
    ListNodel(int x) : key(x), before(NULL), next(NULL) {}
};

class LRUCache{
public: 
    map<int, int> mapkv;
    map<int, ListNodel*> mapkl;
    int capacityq;
    ListNodel * head, *tail;

    LRUCache(int capacity) {
        capacityq = capacity;
        mapkv.clear();
        mapkl.clear();
        head = NULL;
        tail = NULL;
    }

    int get(int key) {
        map<int, int>::iterator it = mapkv.find(key);
        if (it != mapkv.end()) {
            update(key);
            return it->second;
        } else {
            return -1;
        }
    }

    void set(int key, int value) {
        map<int, int>::iterator it = mapkv.find(key);
        if (it != mapkv.end()) {
            it->second = value;
            update(key);
        } else {
            if (mapkv.size() < capacityq) {
                insertKeyValue(key, value);  
            } else {
                ListNodel * tmp = head;
                head = head->next;
                mapkl.erase(tmp->key);
                mapkv.erase(tmp->key);
                delete(tmp);
                insertKeyValue(key, value);
            }
        }
    }

private:
    void insertKeyValue(int key, int value) {
        mapkv.insert(map<int, int>::value_type(key, value));
        if (head == NULL) {
            ListNodel * tmp = new ListNodel(key);
            mapkl.insert(map<int, ListNodel*>::value_type(key, tmp));
            head = tmp;
            tail = tmp;
            head->next = tail;
            tail->before = head;
            tail->next = NULL;
        } else {
            ListNodel * tmp = new ListNodel(key);
            mapkl.insert(map<int, ListNodel*>::value_type(key, tmp));
            tmp->before = tail;
            tail->next = tmp;
            tmp->next = NULL;
            tail = tmp;
        }
    }

    void update(int key) {
        map<int, ListNodel*>::iterator it = mapkl.find(key);
        ListNodel * tmp = it->second;
        if (tmp == head) {
            if (head->next != NULL) {
                head = head->next;
                tail->next = tmp;
                tmp->before = tail;
                tmp->next = NULL;
                tail = tmp;
            }
        } else if (tmp != tail) {
            ListNodel * tmpb = tmp->before;
            ListNodel * tmpn = tmp->next;
            tmpb->next = tmpn;
            tmpn->before = tmpb;
            tmp->before = tail;
            tail->next = tmp;
            tmp->next = NULL;
            tail = tmp;
        }
    }
};
Licensed under CC BY-NC-SA 4.0