-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRUCache.java
More file actions
110 lines (77 loc) · 2.66 KB
/
LRUCache.java
File metadata and controls
110 lines (77 loc) · 2.66 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
102
103
104
105
106
107
108
109
110
class LRUCache {
private class Node {
Integer val;
Integer key;
Node prev;
Node next;
public Node(int key, int val) {
this.key = key;
this.val = val;
this.prev = null;
this.next = null;
}
public String toString() {
return key + " -> " + val;
}
}
private int capacity;
private Map<Integer, Node> cache;
private Node listHead, listLast;
public LRUCache(int capacity) {
assert capacity >= 0 : "capacity is negative";
this.capacity = capacity;
this.cache = new HashMap<>(capacity);
this.listHead = null;
this.listLast = null;
}
/**
Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
*/
public int get(int key) {
assert key >= 0 : "key is negative";
if (!cache.containsKey(key)) return -1;
Node node = cache.get(key);
put(node.key, node.val);
return node.val;
}
/**
Set or insert the value if the key is not already present.
When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
*/
public void put(int key, int value) {
assert key >= 0 : "key is negative";
if (cache.containsKey(key)) {
Node node = cache.get(key);
node.val = value;
removeNode(node);
addFirst(node);
return;
}
if (cache.size() == capacity) {
cache.remove(listLast.key);
removeNode(listLast);
}
Node node = new Node(key, value);
addFirst(node);
cache.put(key, node);
}
private void addFirst(Node node) {
if (listHead != null) listHead.prev = node;
node.next = listHead;
node.prev = null;
listHead = node;
if (listLast == null) listLast = listHead;
}
private void removeNode(Node node) {
if (node.prev == null) listHead = node.next;
else node.prev.next = node.next;
if (node.next == null) listLast = node.prev;
else node.next.prev = node.prev;
}
}
/**
* 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);
*/