0%

LRU 缓存机制

题目链接:
https://leetcode-cn.com/problems/lru-cache/
解法分析:LRU 经典模拟题,使用双链表 + 哈希组合的方法解决。

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/** 
* @param {number} key
* @param {number} value
*/
var DoubleLinkedListNode = function(key, value) {
this.key = key;
this.value = value;
this.prev = null;
this.next = null;
}

/**
* @param {number} capacity
*/
var LRUCache = function(capacity) {
this.capacity = capacity;
this.map = new Map();
// 通过 dummy 节点减少边界处理
this.dummyHead = new DoubleLinkedListNode(null, null);
this.dummyTail = new DoubleLinkedListNode(null, null);
this.dummyHead.next = this.dummyTail;
this.dummyTail.prev = this.dummyHead;
};

/**
* @return {boolean}
*/
LRUCache.prototype._isFull = function() {
return this.map.size === this.capacity;
}

/**
* @param {DoubleLinkedListNode}
* @return {DoubleLinkedListNode}
*/
LRUCache.prototype._removeNode = function(node) {
node.prev.next = node.next;
node.next.prev = node.prev;
// 删除节点
node.prev = null;
node.next = null;
return node;
}

/**
* @param {DoubleLinkedListNode}
*/
LRUCache.prototype._addToHead = function(node) {
const head = this.dummyHead.next;
node.next = head;
head.prev = node;
node.prev = this.dummyHead;
this.dummyHead.next = node;
}

/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function(key) {
if (this.map.has(key)) {
// get 的要放到链表头
const node = this.map.get(key);
this._addToHead(this._removeNode(node));
return node.value;
} else {
return -1;
}
};

/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LRUCache.prototype.put = function(key, value) {
if (this.map.has(key)) {
// 若已存在,修改值,然后将其放到链表头
const node = this.map.get(key);
node.value = value;
this._addToHead(this._removeNode(node));
} else {
if (this._isFull()) {
// 容量已满,去除尾节点,同时删除哈希表中的对应 key
const node = this.dummyTail.prev;
this.map.delete(node.key);
this._removeNode(node);
}
// 添加头节点
const node = new DoubleLinkedListNode(key, value);
this.map.set(key, node);
this._addToHead(node);
}
};

/**
* 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)
*/

本文标题:LRU 缓存机制

文章作者:Flower-F

发布时间:2021年12月24日 - 09:28

最后更新:2022年01月19日 - 16:40

-------------本文结束,感谢您的阅读-------------

欢迎关注我的其它发布渠道