forked from mengli/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRUCache.java
More file actions
Latest commit
152 lines (129 loc) · 3.48 KB
/
Copy pathLRUCache.java
File metadata and controls
152 lines (129 loc) · 3.48 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
importjava.util.HashMap;
importjava.util.Map;
/**
* Design and implement a data structure for Least Recently Used (LRU) cache. It
* should support the following operations: get and set.
*
* get(key) - Get the value (will always be positive) of the key if the key
* exists in the cache, otherwise return -1. set(key, value) - 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.
*
*/
publicclassLRUCache {
publicclassCacheItem<K, V> {
privateKkey;
privateVvalue;
privateCacheItem<K, V> prev;
privateCacheItem<K, V> next;
publicCacheItem<K, V> getPrev() {
returnprev;
}
publicvoidsetPrev(CacheItem<K, V> prev) {
this.prev = prev;
}
publicCacheItem<K, V> getNext() {
returnnext;
}
publicvoidsetNext(CacheItem<K, V> next) {
this.next = next;
}
publicCacheItem(Kkey, Vvalue) {
this.key = key;
this.value = value;
}
}
publicclassBidirectionalList<K, V> {
privateCacheItem<K, V> head = newCacheItem<K, V>(null, null);
privateCacheItem<K, V> tail = head;
publicvoidinsertTail(CacheItem<K, V> node) {
tail.next = node;
node.prev = tail;
node.next = null;
tail = node;
}
publicvoiddelete(CacheItem<K, V> node) {
if (head.next == null)
return;
if (node == head.next) {
deleteHead();
} elseif (node == tail) {
deleteTail();
} else {
node.prev.next = node.next;
node.next.prev = node.prev;
node.next = null;
node.prev = null;
}
}
publicCacheItem<K, V> deleteTail() {
if (head.next == null)
returnnull;
CacheItem<K, V> ret = tail;
tail = tail.prev;
tail.next = null;
ret.prev = null;
ret.next = null;
returnret;
}
publicCacheItem<K, V> deleteHead() {
if (head.next == null)
returnnull;
if (head.next == tail) {
tail = tail.prev;
}
CacheItem<K, V> ret = head.next;
head.next = ret.next;
if (ret.next != null) {
ret.next.prev = head;
}
ret.next = null;
ret.prev = null;
returnret;
}
}
privateMap<Integer, CacheItem<Integer, Integer>> cacheMap = newHashMap<Integer, CacheItem<Integer, Integer>>();
privateBidirectionalList<Integer, Integer> cacheList = newBidirectionalList<Integer, Integer>();
privateintcacheSize = 0;
privateintcapacity;
publicLRUCache(intcapacity) {
this.capacity = capacity;
}
publicintget(intkey) {
CacheItem<Integer, Integer> itm = cacheMap.get(key);
if (itm == null)
return -1;
cacheList.delete(itm);
cacheList.insertTail(itm);
returnitm.value.intValue();
}
publicvoidset(intkey, intvalue) {
if (cacheMap.containsKey(key)) {
updateItem(key, value);
} elseif (cacheSize == capacity && removeLeastUsed()) {
cacheSize--;
addItem(key, value);
} else {
addItem(key, value);
}
}
privatevoidaddItem(intkey, intvalue) {
CacheItem<Integer, Integer> newCacheItem = newCacheItem<Integer, Integer>(
key, value);
cacheList.insertTail(newCacheItem);
cacheMap.put(key, newCacheItem);
cacheSize++;
}
privatevoidupdateItem(intkey, intvalue) {
CacheItem<Integer, Integer> exitedKey = cacheMap.get(key);
exitedKey.value = value;
cacheList.delete(exitedKey);
cacheList.insertTail(exitedKey);
}
privatebooleanremoveLeastUsed() {
CacheItem<Integer, Integer> removedKey = cacheList.deleteHead();
cacheMap.remove(removedKey.key);
returnremovedKey == null ? false : true;
}
}