@@ -106,12 +106,13 @@ class Node {
106
106
107
107
``` python
108
108
class Node :
109
- def __init__ (self , key = 0 , value = 0 ):
109
+ def __init__ (self , key = 0 , val = 0 ):
110
110
self .key = key
111
- self .value = value
111
+ self .val = val
112
112
self .prev = None
113
113
self .next = None
114
114
115
+
115
116
class LRUCache :
116
117
117
118
def __init__ (self , capacity : int ):
@@ -128,12 +129,12 @@ class LRUCache:
128
129
return - 1
129
130
node = self .cache[key]
130
131
self .move_to_head(node)
131
- return node.value
132
+ return node.val
132
133
133
134
def put (self , key : int , value : int ) -> None :
134
135
if key in self .cache:
135
136
node = self .cache[key]
136
- node.value = value
137
+ node.val = value
137
138
self .move_to_head(node)
138
139
else :
139
140
node = Node(key, value)
@@ -155,9 +156,9 @@ class LRUCache:
155
156
156
157
def add_to_head (self , node ):
157
158
node.next = self .head.next
158
- self .head.next.prev = node
159
- self .head.next = node
160
159
node.prev = self .head
160
+ self .head.next = node
161
+ node.next.prev = node
161
162
162
163
def remove_tail (self ):
163
164
node = self .tail.prev
@@ -176,49 +177,48 @@ class LRUCache:
176
177
<!-- 这里可写当前语言的特殊实现逻辑 -->
177
178
178
179
``` java
179
- class LRUCache {
180
- class Node {
181
- int key;
182
- int value;
183
- Node prev;
184
- Node next;
185
- Node () {
180
+ class Node {
181
+ int key;
182
+ int val;
183
+ Node prev;
184
+ Node next;
186
185
187
- }
188
- Node (int key , int value ) {
189
- this . key = key;
190
- this . value = value;
191
- }
186
+ Node () {
187
+
188
+ }
189
+
190
+ Node (int key , int val ) {
191
+ this . key = key;
192
+ this . val = val;
192
193
}
194
+ }
193
195
194
- private Map<Integer , Node > cache;
195
- private Node head;
196
- private Node tail;
196
+ class LRUCache {
197
+ private Map<Integer , Node > cache = new HashMap<> ();
198
+ private Node head = new Node ();
199
+ private Node tail = new Node ();
197
200
private int capacity;
198
201
private int size;
199
202
200
203
public LRUCache (int capacity ) {
201
- cache = new HashMap<> ();
202
204
this . capacity = capacity;
203
- head = new Node ();
204
- tail = new Node ();
205
205
head. next = tail;
206
206
tail. prev = head;
207
207
}
208
-
208
+
209
209
public int get (int key ) {
210
210
if (! cache. containsKey(key)) {
211
211
return - 1 ;
212
212
}
213
213
Node node = cache. get(key);
214
214
moveToHead(node);
215
- return node. value ;
215
+ return node. val ;
216
216
}
217
-
217
+
218
218
public void put (int key , int value ) {
219
219
if (cache. containsKey(key)) {
220
220
Node node = cache. get(key);
221
- node. value = value;
221
+ node. val = value;
222
222
moveToHead(node);
223
223
} else {
224
224
Node node = new Node (key, value);
@@ -245,9 +245,9 @@ class LRUCache {
245
245
246
246
private void addToHead (Node node ) {
247
247
node. next = head. next;
248
- head. next. prev = node;
249
- head. next = node;
250
248
node. prev = head;
249
+ head. next = node;
250
+ node. next. prev = node;
251
251
}
252
252
253
253
private Node removeTail () {
@@ -465,6 +465,96 @@ func (this *LRUCache) pushFront(n *node) {
465
465
}
466
466
```
467
467
468
+ ### ** C++**
469
+
470
+ ``` cpp
471
+ struct Node {
472
+ int k;
473
+ int v;
474
+ Node* prev;
475
+ Node* next;
476
+
477
+ Node(): k(0), v(0), prev(nullptr), next(nullptr) {}
478
+ Node (int key, int val): k(key), v(val), prev(nullptr), next(nullptr) {}
479
+ };
480
+
481
+ class LRUCache {
482
+ public:
483
+ LRUCache(int capacity): cap(capacity), size(0) {
484
+ head = new Node();
485
+ tail = new Node();
486
+ head->next = tail;
487
+ tail->prev = head;
488
+ }
489
+
490
+ int get(int key) {
491
+ if (!cache.count(key)) return -1;
492
+ Node* node = cache[ key] ;
493
+ moveToHead(node);
494
+ return node->v;
495
+ }
496
+
497
+ void put(int key, int value) {
498
+ if (cache.count(key))
499
+ {
500
+ Node* node = cache[ key] ;
501
+ node->v = value;
502
+ moveToHead(node);
503
+ }
504
+ else
505
+ {
506
+ Node* node = new Node(key, value);
507
+ cache[ key] = node;
508
+ addToHead(node);
509
+ ++size;
510
+ if (size > cap)
511
+ {
512
+ node = removeTail();
513
+ cache.erase(node->k);
514
+ --size;
515
+ }
516
+ }
517
+ }
518
+
519
+ private:
520
+ unordered_map<int, Node* > cache;
521
+ Node* head;
522
+ Node* tail;
523
+ int cap;
524
+ int size;
525
+
526
+ void moveToHead(Node* node) {
527
+ removeNode(node);
528
+ addToHead(node);
529
+ }
530
+
531
+ void removeNode(Node* node) {
532
+ node->prev->next = node->next;
533
+ node->next->prev = node->prev;
534
+ }
535
+
536
+ void addToHead(Node* node) {
537
+ node->next = head->next;
538
+ node->prev = head;
539
+ head->next = node;
540
+ node->next->prev = node;
541
+ }
542
+
543
+ Node* removeTail() {
544
+ Node* node = tail->prev;
545
+ removeNode(node);
546
+ return node;
547
+ }
548
+ };
549
+
550
+ /**
551
+ * Your LRUCache object will be instantiated and called as such:
552
+ * LRUCache* obj = new LRUCache(capacity);
553
+ * int param_1 = obj->get(key);
554
+ * obj->put(key,value);
555
+ * /
556
+ ```
557
+
468
558
### **...**
469
559
470
560
```
0 commit comments