-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path146. LRU Cache.js
63 lines (54 loc) · 1.53 KB
/
146. LRU Cache.js
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
var LRUCache = function (capacity) {
this.capacity = capacity;
this.cache = new Array();
};
LRUCache.prototype.get = function (key) {
for (let i = 0; i < this.cache.length; ++i) {
if (this.cache[i][0] === key) {
let entry = this.cache[i];
this.cache.splice(i, 1);
this.cache.push(entry);
return entry[1];
}
}
return -1;
};
LRUCache.prototype.put = function (key, value) {
for (let i = 0; i < this.cache.length; ++i) {
if (this.cache[i][0] === key) {
let entry = this.cache[i];
this.cache.splice(i, 1);
entry[1] = value;
this.cache.push(entry);
return entry[1];
}
}
if (this.cache.length === this.capacity) {
this.cache.shift();
}
this.cache.push([key, value]);
};
/**********************************************************************************************/
/**
* Another solution using Map()
* Copied from: https://leetcode.com/problems/lru-cache/discuss/399146/Clean-JavaScript-solution
* It works since JavaScript Map is an orderedDict.
*/
var LRUCache = function (capacity) {
this.capacity = capacity;
this.cache = new Map();
};
LRUCache.prototype.get = function (key) {
if (!this.cache.has(key)) return -1;
let value = this.cache.get(key);
this.cache.delete(key);
this.cache.set(key, value);
return value;
};
LRUCache.prototype.put = function (key, value) {
if (this.cache.has(key)) this.cache.delete(key);
this.cache.set(key, value);
if (this.cache.size > this.capacity) {
this.cache.delete(this.cache.keys().next().value);
}
};