-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRUCache.java
More file actions
84 lines (74 loc) · 1.91 KB
/
Copy pathLRUCache.java
File metadata and controls
84 lines (74 loc) · 1.91 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
import java.util.Hashtable;
public class LRUCache {
private class DLinkedNode{
int key;
int value;
DLinkedNode pre;
DLinkedNode next;
public DLinkedNode(int k,int v){
key = k;
value = v;
}
}
int capacity = 0;
int elementsNumber = 0;
// here head and tail are always not null
DLinkedNode head = new DLinkedNode(0,0);
DLinkedNode tail = new DLinkedNode(0,0);
Hashtable<Integer,DLinkedNode> ht;
public LRUCache(int capacity) {
this.capacity = capacity;
ht = new Hashtable<Integer,DLinkedNode>();
this.head.next = tail;
this.tail.pre = head;
}
public int get(int key) {
if (!ht.containsKey(key)) {
return -1;
}
DLinkedNode node = ht.get(key);
remove(node);
addToTheHead(node);
ht.put(key,head.next);
return this.head.next.value;
}
public void put(int key, int value) {
if (ht.containsKey(key)) {
DLinkedNode node = ht.get(key);
node.value = value;
remove(node);
addToTheHead(node);
}
else{
if (this.elementsNumber<this.capacity) {
DLinkedNode node = new DLinkedNode(key,value);
addToTheHead(node);
ht.put(key,node);
this.elementsNumber++;
}
else {
ht.remove(this.tail.pre.key);
remove(this.tail.pre);
DLinkedNode node = new DLinkedNode(key,value);
addToTheHead(node);
ht.put(key,node);
}
}
}
private void addToTheHead(DLinkedNode node){
node.next = head.next;
node.pre = head;
head.next.pre = node;
head.next = node;
}
private void remove(DLinkedNode node){
node.pre.next = node.next;
node.next.pre = node.pre;
}
}
/**
* 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);
*/