-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRUCache.java
More file actions
90 lines (80 loc) · 2.55 KB
/
LRUCache.java
File metadata and controls
90 lines (80 loc) · 2.55 KB
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
package leetcode._146_;
import java.util.HashMap;
import java.util.Map;
/**
* Created by zhangbo54 on 2019-07-23.
*/
class LRUCache {
class LRUValue {
int key;
int value;
LRUValue next; //链表下一个节点的key
LRUValue pre;// 链表上一个节点的key
public LRUValue(int key, int value, LRUValue next, LRUValue pre) {
this.key = key;
this.value = value;
this.next = next;
this.pre = pre;
}
}
private Map<Integer, LRUValue> map = new HashMap<>();
private int capacity;
private LRUValue head;
public LRUCache(int capacity) {
this.capacity = capacity;
}
public int get(int key) {
if (!map.containsKey(key)) {
return -1;
} else {
LRUValue lruValue = map.get(key);
if (lruValue != head) { // 如果获取的元素不是在头部,需要将该元素升级到头部
promotionNode(lruValue);
}
return head.value;
}
}
public void put(int key, int value) {
if (map.size() == 0) {
LRUValue lruValue = new LRUValue(key, value, null, null);
lruValue.next = lruValue;
lruValue.pre = lruValue;
head = lruValue;
map.put(key, lruValue);
} else {
LRUValue targetValue = map.get(key);
if (targetValue != null) {// 注意 put 也算是访问,需要把它提到 header
if (targetValue != head) {
promotionNode(targetValue);
}
targetValue.value = value;
} else {
if (map.size() >= capacity) {
int removeKey = head.pre.key;
head.pre = head.pre.pre;
map.remove(removeKey);
}
LRUValue lruValue = new LRUValue(key, value, head, head.pre);
map.put(key, lruValue);
head.pre.next = lruValue;
head.pre = lruValue;
head = lruValue;
}
}
}
private void promotionNode(LRUValue lruValue) {
lruValue.pre.next = lruValue.next;
lruValue.next.pre = lruValue.pre;
head.pre.next = lruValue;
lruValue.next = head;
lruValue.pre = head.pre;
head.pre = lruValue;
head = lruValue;
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/