-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path146.lru-cache.java
More file actions
101 lines (88 loc) · 2.6 KB
/
146.lru-cache.java
File metadata and controls
101 lines (88 loc) · 2.6 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
91
92
93
94
95
96
97
98
99
100
101
public class LRUCache {
class DLinkedNode {
int key;
int value;
DLinkedNode pre;
DLinkedNode post;
}
/**
* Always add the new node right after head;
*/
private void addNode(DLinkedNode node){
node.pre = this.head;
node.post = this.head.post;
this.head.post.pre = node;
this.head.post = node;
}
/**
* Remove an existing node from the linked list.
*/
private void removeNode(DLinkedNode node){
node.pre.post = node.post;
node.post.pre = node.pre;
}
/**
* Move certain node in between to the head.
*/
private void moveToHead(DLinkedNode node){
this.removeNode(node);
this.addNode(node);
}
// pop the current tail.
private DLinkedNode popTail(){
DLinkedNode result = tail.pre;
this.removeNode(result);
return result;
}
private HashMap<Integer,DLinkedNode> cache = new HashMap<>();
private int count;
private int capacity;
private DLinkedNode head,tail;
public LRUCache(int capacity) {
this.count=0;
this.capacity=capacity;
this.head = new DLinkedNode();
this.head.pre = null;
this.tail = new DLinkedNode();
this.tail.post = null;
this.head.post = tail;
this.tail.pre = head;
}
public int get(int key) {
if (cache.containsKey(key)){
this.moveToHead(this.cache.get(key));
return this.cache.get(key).value;
}else {
return -1;
}
}
public void put(int key, int value) {
if (this.cache.containsKey(key)){
DLinkedNode removeone = this.cache.get(key);
this.removeNode(removeone);
DLinkedNode node = new DLinkedNode();
node.value = value;
node.key = key;
this.cache.put(key,node);
this.addNode(node);
}else {
if (this.count==this.capacity){
DLinkedNode removeone = this.popTail();
this.cache.remove(removeone.key);
}else {
this.count++;
}
DLinkedNode node = new DLinkedNode();
node.value = value;
node.key = key;
this.cache.put(key,node);
this.addNode(node);
}
}
}
/**
* 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);
*/