C++ 写法

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    struct node{
int key;
int value;
struct node * left;
struct node * right;
node(int k,int v){
key = k;
value = v;
left = NULL;
right = NULL;
}
};

class LRUCache {
public:

struct node * head;
struct node * tail;
int capacity = 0;
int current_size = 0;

LRUCache(int capacity) {
this->capacity = capacity;
head = new node(0,0);
tail = new node(0,0);
head -> left = tail;
head -> right = tail;
tail -> left = head;
tail -> right = head;
}

int get(int key) {
struct node * ptr = head -> right;
while (ptr != NULL && ptr != tail) {
if( ptr -> key == key ){
int value = ptr->value;
delete_node(ptr);
ptr = new node(key,value);
add_to_head(ptr);
return value;
}
ptr = ptr -> right;
}
return -1;
}

void put(int key, int value) {
struct node * ptr = head->right;
bool exists = false;
if( current_size > 0){
while (ptr != NULL && ptr != tail) {
if( ptr -> key == key ){
delete_node(ptr);
ptr = new node(key,value);
add_to_head(ptr);
exists = true;
break;
}
ptr = ptr -> right;
}
}
if(!exists){
if( current_size < capacity ){
current_size ++;
}else{
delete_tail();
}
ptr = new node(key,value);
add_to_head(ptr);
}
}

private:

void delete_node( struct node * ptr ){
if( ptr != NULL ){
ptr->left->right = ptr->right;
ptr->right->left = ptr->left;
delete ptr;
}
}

void delete_tail( ){
if( tail != NULL && tail -> left != head ){
delete_node(tail->left);
}
}

void add_to_head( struct node * ptr ){
ptr->right = head -> right;
ptr->left = head;
head -> right = ptr;
ptr -> right -> left = ptr;
}

};

Java 实现:

class LRUCache {

Cache head = null;
Cache tail = null;
int currentSize = 0;
int capacity = 0;

public LRUCache(int capacity) {
head = new Cache(0, 0);
tail = new Cache(0, 0);
head.left = tail;
head.right = tail;
tail.left = head;
tail.right = head;
this.capacity = capacity;
}

public int get(int key) {
Cache ptr = head.right;
while (ptr != tail) {
if (ptr.key == key) {
ptr.delete();
ptr.right = head.right;
ptr.left = head;
head.right = ptr;
ptr.right.left = ptr;
return ptr.value;
}
ptr = ptr.right;
}
return -1;
}

public void put(int key, int value) {
boolean isFind = false;
if (currentSize > 0) {
Cache ptr = head.right;
while (ptr != null && ptr != tail) {
if (ptr.key == key) {
ptr.delete();
// Insert
ptr.right = head.right;
ptr.left = head;
ptr.right.left = ptr;
head.right = ptr;
// Value
ptr.value = value;
isFind = true;
break;
}
ptr = ptr.right;
}
}
if (!isFind) {
Cache cache = new Cache(key, value);
if (currentSize >= capacity) {
Cache deletePtr = tail.left;
deletePtr.delete();
} else {
currentSize++;
}
Cache ptr = head.right;
head.right = cache;
cache.left = head;
cache.right = ptr;
ptr.left = cache;
}
}

private class Cache {
int key;
int value;

Cache left = null;
Cache right = null;

Cache(int key, int value) {
this.key = key;
this.value = value;
}

void delete() {
if( this.left != null ) {
this.left.right = this.right;
}
if(this.right != null ) {
this.right.left = this.left;
}
this.left = null;
this.right = null;
}
}
}