-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path146-2.java
83 lines (72 loc) · 1.81 KB
/
146-2.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Node {
int key, value;
Node next, prev;
public Node() {}
public Node(int key, int value) {
this.key = key;
this.value = value;
next = null;
prev = null;
}
}
class LRUCache {
Map<Integer, Node> map;
int capacity;
int size;
Node head;
Node tail;
public LRUCache(int capacity) {
map = new HashMap<>();
this.capacity = capacity;
this.size = 0;
head = new Node();
tail = new Node();
head.next = tail;
tail.prev = head;
}
public int get(int key) {
if (!map.containsKey(key)) return -1;
Node node = map.get(key);
moveToHead(node);
return node.value;
}
public void put(int key, int value) {
if (map.containsKey(key)) {
map.get(key).value = value;
moveToHead(map.get(key));
} else {
Node newNode = new Node(key, value);
map.put(key, newNode);
addToHead(newNode);
size++;
if (size > capacity) {
Node removeNode = removeTail();
map.remove(removeNode.key);
size--;
}
}
}
private void addToHead(Node node) {
node.next = head.next;
node.prev = head;
head.next.prev = node;
head.next = node;
}
private void moveToHead(Node node) {
node.prev.next = node.next;
node.next.prev = node.prev;
addToHead(node);
}
private Node removeTail() {
Node res = tail.prev;
res.prev.next = tail;
tail.prev = res.prev;
return res;
}
}
/**
* 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);
*/