-
Notifications
You must be signed in to change notification settings - Fork 0
/
200213-1.cpp
150 lines (148 loc) · 3.92 KB
/
200213-1.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// https://leetcode-cn.com/problems/lru-cache/
#include <cstdio>
#include <list>
#include <unordered_map>
using namespace std;
class LRUCache {
public:
LRUCache(int capacity): capacity_(capacity), head(NULL), tail(NULL) {
//printf("===> init(%d)\n", capacity);
}
~LRUCache() {
for (Node* p = head; p;) {
Node* q = p->next;
delete p;
p = q;
}
}
int get(int key) {
//printf("===> get(%d)\n", key);
auto it = m.find(key);
if (it == m.end()) return -1;
Node* p = it->second;
if (p != head) {
if (p == tail) tail = tail->prev;
if (p->next) p->next->prev = p->prev;
if (p->prev) p->prev->next = p->next;
p->next = head;
head->prev = p;
head = p;
}
//dump();
return p->val;
}
void put(int key, int value) {
//printf("===> put(%d, %d)\n", key, value);
auto it = m.find(key);
Node* p;
if (it != m.end()) {
p = it->second;
p->val = value;
if (p != head) {
if (p == tail) tail = tail->prev;
if (p->next) p->next->prev = p->prev;
if (p->prev) p->prev->next = p->next;
p->next = head;
if (head) head->prev = p;
head = p;
if (!tail) tail = p;
}
} else {
while (m.size() >= capacity_) {
if (tail && tail->prev) {
Node* p = tail;
tail = p->prev;
tail->next = NULL;
m.erase(m.find(p->key));
delete p;
} else {
delete head;
m.clear();
head = tail = NULL;
}
}
//dump();
//printf("---\n");
p = new Node(key, value);
m[key] = p;
p->next = head;
if (head) head->prev = p;
head = p;
if (!tail) tail = p;
}
//dump();
}
private:
void dump() {
printf("head: %p (%d), tail: %p (%d)\n", head, (head ? head->val : -1), tail, (tail ? tail->val : -1));
printf("map: [ "); for (auto it = m.begin(); it != m.end(); ++it) printf("{%d,%d,%d} ", it->first, it->second->key, it->second->val); printf("]\n");
printf("list: [ "); for (Node* p = head; p; p = p->next) printf("{%d,%d} ", p->key, p->val); printf("]\n");
}
struct Node {
int key, val;
Node *prev, *next;
Node(int k, int v): key(k), val(v), prev(NULL), next(NULL) { }
};
int capacity_;
unordered_map<int, Node*> m;
Node *head, *tail;
};
/**
* 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);
*/
int main()
{
{
LRUCache cache( 2 /* 缓存容量 */ );
cache.put(1, 1);
cache.put(2, 2);
printf("%d\n", cache.get(1)); // 返回 1
cache.put(3, 3); // 该操作会使得密钥 2 作废
printf("%d\n", cache.get(2)); // 返回 -1 (未找到)
cache.put(4, 4); // 该操作会使得密钥 1 作废
printf("%d\n", cache.get(1)); // 返回 -1 (未找到)
printf("%d\n", cache.get(3)); // 返回 3
printf("%d\n", cache.get(4)); // 返回 4
}
{
LRUCache cache( 2 /* 缓存容量 */ );
cache.put(2, 1);
printf("%d\n", cache.get(2)); // 返回 1
cache.put(3, 2);
printf("%d\n", cache.get(2)); // 返回 1
printf("%d\n", cache.get(3)); // 返回 2
}
{
LRUCache cache( 1 /* 缓存容量 */ );
cache.put(2, 1);
printf("%d\n", cache.get(2)); // 返回 1
cache.put(3, 2);
printf("%d\n", cache.get(2)); // 返回 1
printf("%d\n", cache.get(3)); // 返回 2
}
{
LRUCache cache( 2 /* 缓存容量 */ );
printf("%d\n", cache.get(2)); // 返回 -1
cache.put(2, 6);
printf("%d\n", cache.get(1)); // 返回 -1
cache.put(1, 5);
cache.put(1, 2);
printf("%d\n", cache.get(1)); // 返回 2
printf("%d\n", cache.get(2)); // 返回 6
}
{
LRUCache cache( 2 /* 缓存容量 */ );
cache.put(2, 1);
cache.put(3, 2);
printf("%d\n", cache.get(3)); // 返回 2
printf("%d\n", cache.get(2)); // 返回 1
cache.put(4, 3); // 该操作会使得密钥 3 作废
printf("%d\n", cache.get(2)); // 返回 1
printf("%d\n", cache.get(3)); // 返回 -1
printf("%d\n", cache.get(4)); // 返回 3
}
return 0;
}