LeetCode 146. LRU 缓存
题目描述
思路分析
- Java 通过继承
LinkedHashMap
实现- Golang 通过使用
container/list
和map
来实现- 自定义双向链表+哈希表
参考代码
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
type LRUCache struct {
capacity int
cache map[int]*Node
head *Node
tail *Node
}
type Node struct {
key int
value int
prev *Node
next *Node
}
func Constructor(capacity int) LRUCache {
head := &Node{}
tail := &Node{}
head.next = tail
tail.prev = head
return LRUCache{
capacity: capacity,
cache: make(map[int]*Node),
head: head,
tail: tail,
}
}
func (this *LRUCache) Get(key int) int {
if node, ok := this.cache[key]; ok {
this.moveToHead(node)
return node.value
}
return -1
}
func (this *LRUCache) Put(key int, value int) {
if node, ok := this.cache[key]; ok {
node.value = value
this.moveToHead(node)
} else {
if len(this.cache) >= this.capacity {
this.removeTail()
}
newNode := &Node{key: key, value: value}
this.cache[key] = newNode
this.addToHead(newNode)
}
}
func (this *LRUCache) moveToHead(node *Node) {
this.removeNode(node)
this.addToHead(node)
}
func (this *LRUCache) removeNode(node *Node) {
node.prev.next = node.next
node.next.prev = node.prev
}
func (this *LRUCache) addToHead(node *Node) {
node.prev = this.head
node.next = this.head.next
this.head.next.prev = node
this.head.next = node
}
func (this *LRUCache) removeTail() {
lastNode := this.tail.prev
this.removeNode(lastNode)
delete(this.cache, lastNode.key)
}
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
class Node {
int key, val;
Node next, prev;
public Node() {
}
public Node(int key, int val) {
this.key = key;
this.val = val;
}
}
class LRUCache {
private final int capacity;
private final Map<Integer, Node> cache;
private final Node head;
private final Node tail;
public LRUCache(int capacity) {
this.capacity = capacity;
this.cache = new HashMap<>();
this.head = new Node();
this.tail = new Node();
this.head.next = this.tail;
this.tail.prev = this.head;
}
public int get(int key) {
if (cache.containsKey(key)) {
Node node = cache.get(key);
moveToHead(node);
return node.val;
}
return -1;
}
public void put(int key, int val) {
if (cache.containsKey(key)) {
Node node = cache.get(key);
moveToHead(node);
node.val = val;
} else {
if (cache.size() >= capacity) {
Node lastNode = removeTail();
cache.remove(lastNode.key);
}
Node newNode = new Node(key, val);
cache.put(key, newNode);
addToHead(newNode);
}
}
// 将节点移到头部,表示最近使用
private void moveToHead(Node node) {
removeNode(node);
addToHead(node);
}
// 移除节点
private void removeNode(Node node) {
node.next.prev = node.prev;
node.prev.next = node.next;
}
// 移除尾部节点,返回被移除的节点
private Node removeTail() {
Node lastNode = tail.prev;
removeNode(lastNode);
return lastNode;
}
// 将节点添加到头部,表示最近使用
private void addToHead(Node node) {
node.next = head.next;
node.prev = head;
head.next.prev = node;
head.next = node;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class LRUCache extends LinkedHashMap<Integer, Integer> {
private final int capacity;
public LRUCache(int capacity) {
super(capacity, 0.75f, true);
this.capacity = capacity;
}
public int get(int key) {
return this.getOrDefault(key, -1);
}
public void put(int key, int value) {
super.put(key, value);
}
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return this.size() > capacity;
}
}
CC BY-NC-SA 4.0
许可协议,转载请注明出处!
本博客所有文章除特别声明外,均采用