-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path146_lru-cache.cpp
More file actions
81 lines (74 loc) · 1.84 KB
/
146_lru-cache.cpp
File metadata and controls
81 lines (74 loc) · 1.84 KB
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
class LRUCache {
int cap;
struct Node {
int key, val;
Node *pre, *nxt;
Node(int k, int v) : key(k), val(v), pre(NULL), nxt(NULL) {}
} *head = NULL, *tail = NULL;
unordered_map<int, Node*> m;
public:
LRUCache(int capacity) { cap = capacity; }
void move_head(Node* p) {
// printf("move head %d %d\n", p->key, p->val);
if (p == head) {
return;
}
auto pre = p->pre;
auto nxt = p->nxt;
if (pre) {
pre->nxt = nxt;
}
if (nxt) {
nxt->pre = pre;
}
p->pre = NULL;
p->nxt = head;
if (head)
head->pre = p;
head = p;
if (tail == p) {
tail = pre;
}
}
int get(int key) {
// printf("query %d\n", key);
if (m.count(key)) {
move_head(m[key]);
return m[key]->val;
}
return -1;
}
void put(int key, int value) {
if (m.count(key)) {
m[key]->val = value;
move_head(m[key]);
// printf("change %d %d\n", key, value);
return;
}
// printf("add %d %d\n", key, value);
auto tmp = new Node(key, value);
tmp->nxt = head;
if (head)
head->pre = tmp;
head = tmp;
if (!tail)
tail = tmp;
m[key] = tmp;
if (m.size() > cap) {
auto tmp = tail;
// printf("del %d %d\n", tmp->key, tmp->val);
tail = tail->pre;
if (tail) {
tail->nxt = NULL;
}
m.erase(tmp->key);
delete tmp;
}
}
};
/**
* 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);
*/