-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path146_LRUCache.js
More file actions
46 lines (42 loc) · 958 Bytes
/
Copy path146_LRUCache.js
File metadata and controls
46 lines (42 loc) · 958 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
/**
* https://leetcode.cn/problems/lru-cache/
* LRU 缓存
*
* medium
*
* 思路:利用 Map 的特点。m.delete(m.keys().next().value) 可删除 map 的最早项
*/
class LRUCache {
constructor(size) {
this.cache = new Map();
this.SIZE = size;
}
get(key) {
if (!this.cache.has(key)) return -1;
const val = this.cache.get(key);
this.cache.delete(key);
this.cache.set(key, val);
return val;
}
put(key, val) {
if (this.cache.has(key)) {
this.cache.delete(key);
}
this.cache.set(key, val);
if (this.cache.size > this.SIZE) {
const keys = this.cache.keys();
this.cache.delete(keys.next().value);
}
}
}
// ----
const cache = new LRUCache(2);
cache.put(1, 1);
cache.put(2, 2);
console.log(cache.get(1)); // 1
cache.put(3, 3);
console.log(cache.get(2)); // -1
cache.put(4, 4);
console.log(cache.get(1)); // -1
console.log(cache.get(3)); // 3
console.log(cache.get(4)); // 4