@@ -110,72 +110,85 @@ function put (key, value) {
110
110
JS Code:
111
111
112
112
``` js
113
- function ListNode (key , val ) {
114
- this .key = key;
115
- this .val = val;
116
- this .pre = this .next = null ;
117
- }
118
-
119
- var LRUCache = function (capacity ) {
120
- this .capacity = capacity;
121
- this .size = 0 ;
122
- this .data = {};
123
- this .head = new ListNode ();
124
- this .tail = new ListNode ();
125
- this .head .next = this .tail ;
126
- this .tail .pre = this .head ;
113
+ class ListNode {
114
+ constructor (key , val ){
115
+ this .key = key;
116
+ this .val = val;
117
+ this .pre = null ;
118
+ this .next = null ;
119
+ }
127
120
};
128
121
129
- function get ( key ) {
130
- if ( this . data [key] !== undefined ) {
131
- let node = this .data [key] ;
132
- this .removeNode (node) ;
133
- this .appendHead (node) ;
134
- return node . val ;
135
- } else {
136
- return - 1 ;
137
- }
138
- }
122
+ class LRUCache {
123
+ constructor ( capacity ) {
124
+ this .capacity = capacity ;
125
+ this .size = 0 ;
126
+ this .data = {} ;
127
+ this . head = new ListNode () ;
128
+ this . tail = new ListNode ();
129
+ this . head . next = this . tail ;
130
+ this . tail . pre = this . head ;
131
+ }
139
132
140
- function put (key , value ) {
141
- let node;
142
- if (this .data [key] !== undefined ) {
143
- node = this .data [key];
144
- this .removeNode (node);
145
- node .val = value;
146
- } else {
147
- node = new ListNode (key, value);
148
- this .data [key] = node;
149
- if (this .size < this .capacity ) {
150
- this .size ++ ;
151
- } else {
152
- key = this .removeTail ();
153
- delete this .data [key];
133
+ get (key ){
134
+ if (! this .data [key]) return - 1 ;
135
+ else {
136
+ let node = this .data [key];
137
+ this .removeNode (node);
138
+ this .appendHead (node);
139
+
140
+ return node .val ;
141
+ }
154
142
}
155
- }
156
- this .appendHead (node);
157
- }
158
143
159
- function removeNode (node ) {
160
- let preNode = node .pre ,
161
- nextNode = node .next ;
162
- preNode .next = nextNode;
163
- nextNode .pre = preNode;
164
- }
144
+ put (key , value ){
145
+ if (! this .data [key]){
146
+ let node = new ListNode (key, value);
165
147
166
- function appendHead (node ) {
167
- let firstNode = this .head .next ;
168
- this .head .next = node;
169
- node .pre = this .head ;
170
- node .next = firstNode;
171
- firstNode .pre = node;
172
- }
148
+ this .data [key] = node;
149
+ this .appendHead (node);
150
+ this .size ++ ;
151
+
152
+ if (this .size > this .capacity ){
153
+ const lastKey = this .removeTail ();
154
+ delete this .data [lastKey];
155
+ this .size -- ;
156
+ }
173
157
174
- function removeTail () {
175
- let key = this .tail .pre .key ;
176
- this .removeNode (this .tail .pre );
177
- return key;
158
+ }else {
159
+ let node = this .data [key];
160
+ this .removeNode (node);
161
+ node .val = value;
162
+ this .appendHead (node);
163
+ }
164
+ }
165
+
166
+ removeNode (node ){
167
+ let preNode = node .pre ;
168
+ let nextNode = node .next ;
169
+
170
+ preNode .next = nextNode;
171
+ nextNode .pre = preNode;
172
+ }
173
+
174
+ appendHead (node ){
175
+ let firstNode = this .head .next ;
176
+
177
+ this .head .next = node;
178
+ node .pre = this .head ;
179
+ node .next = firstNode;
180
+ firstNode .pre = node;
181
+ }
182
+
183
+ removeTail (){
184
+ let key = this .tail .pre .key ;
185
+
186
+ this .removeNode (this .tail .pre );
187
+
188
+ return key;
189
+ }
178
190
}
191
+
179
192
```
180
193
181
194
Go Code:
0 commit comments