-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path146.js
More file actions
49 lines (46 loc) · 988 Bytes
/
146.js
File metadata and controls
49 lines (46 loc) · 988 Bytes
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
/**
* @param {number} capacity
*/
var LRUCache = function(capacity) {
this.capacity = capacity;
this.cache = {};
this.keys = [];
};
/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function(key) {
const index = this.keys.indexOf(key);
if (index !== -1) {
this.keys.splice(index, 1);
this.keys.push(key);
return this.cache[key];
} else {
return -1;
}
};
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LRUCache.prototype.put = function(key, value) {
const index = this.keys.indexOf(key);
if (index !== -1) {
this.keys.splice(index, 1);
this.keys.push(key);
} else {
if (this.keys.length === this.capacity) {
this.keys.shift();
}
this.keys.push(key);
}
this.cache[key] = value;
};
/**
* Your LRUCache object will be instantiated and called as such:
* var obj = Object.create(LRUCache).createNew(capacity)
* var param_1 = obj.get(key)
* obj.put(key,value)
*/