-
Notifications
You must be signed in to change notification settings - Fork 0
/
146. LRU缓存机制.js
68 lines (62 loc) · 1.57 KB
/
146. LRU缓存机制.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
64
65
66
67
68
/**
* @param {number} capacity
*/
var LRUCache = class {
constructor(capacity) {
this.cache = new Map();
this.capacity = capacity;
}
/**
* @param {number} key
* @return {number}
*/
get(key) {
let cache = this.cache;
if (cache.has(key)) {
let value = cache.get(key)
cache.delete(key);
cache.set(key, value);
return value;
} else {
return -1;
}
};
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
put(key, value) {
let cache = this.cache;
if (cache.has(key)) {
cache.delete(key);
} else if (cache.size >= this.capacity) {
cache.delete(cache.keys().next().value);
}
cache.set(key, value);
};
};
/**
* Your LRUCache object will be instantiated and called as such:
* var obj = new LRUCache(capacity)
* var param_1 = obj.get(key)
* obj.put(key,value)
*/
let cache = new LRUCache(2 /* 缓存容量 */);
// cache.get(2);
// cache.put(2, 6);
// cache.get(1); // 返回 -1
// cache.put(1, 5); // 该操作会使得关键字 2 作废
// cache.put(1, 2); // 该操作会使得关键字 1 作废
// cache.get(1); // 返回 -1 (未找到)
// cache.get(2); // 返回 3
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // 返回 1
cache.put(3, 3); // 该操作会使得关键字 2 作废
cache.get(2); // 返回 -1 (未找到)
cache.put(4, 4); // 该操作会使得关键字 1 作废
cache.get(1); // 返回 -1 (未找到)
cache.get(3); // 返回 3
cache.get(4); // 返回 4
console.log([...cache.cache])