-
Notifications
You must be signed in to change notification settings - Fork 9
/
LRUCache.java
64 lines (57 loc) · 1.46 KB
/
LRUCache.java
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
class Node {
int key;
int val;
Node prev;
Node next;
public Node(int k, int v) {
key = k;
val = v;
}
}
public class LRUCache {
private int capacity;
private Map<Integer, Node> map = new HashMap<>();
Node head = null;
Node tail = null;
public LRUCache(int capacity) {
this.capacity = capacity;
}
public int get(int key) {
if (!map.containsKey(key)) return -1;
Node node = map.get(key);
remove(node);
setHead(node);
return node.val;
}
public void set(int key, int value) {
if (map.containsKey(key)) {
Node node = map.get(key);
node.val = value;
remove(node);
setHead(node);
} else {
Node node = new Node(key, value);
if (map.size() == capacity) {
map.remove(tail.key);
remove(tail);
setHead(node);
} else {
setHead(node);
}
map.put(key, node);
}
}
public void remove(Node node) {
if (node.prev != null) node.prev.next = node.next;
else head = node.next;
if (node.next != null) node.next.prev = node.prev;
else tail = node.prev;
}
public void setHead(Node node) {
node.next = head;
node.prev = null;
if (head != null) head.prev = node;
head = node;
if (tail == null) tail = head;
}
}