Skip to content

Commit 592320c

Browse files
update 146
1 parent 5292c3a commit 592320c

File tree

1 file changed

+3
-29
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+3
-29
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_146.java

+3-29
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,6 @@
44
import java.util.LinkedHashMap;
55
import java.util.Map;
66

7-
/**
8-
* 146. LRU Cache
9-
* <p>
10-
* Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
11-
* <p>
12-
* get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
13-
* put(key, value) - Set or insert the value if the key is not already present.
14-
* When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
15-
* <p>
16-
* Follow up:
17-
* Could you do both operations in O(1) time complexity?
18-
* <p>
19-
* Example:
20-
* <p>
21-
* LRUCache cache = new LRUCache(2);//capacity
22-
* <p>
23-
* cache.put(1, 1);
24-
* cache.put(2, 2);
25-
* cache.get(1); // returns 1
26-
* cache.put(3, 3); // evicts key 2
27-
* cache.get(2); // returns -1 (not found)
28-
* cache.put(4, 4); // evicts key 1
29-
* cache.get(1); // returns -1 (not found)
30-
* cache.get(3); // returns 3
31-
* cache.get(4); // returns 4
32-
*/
33-
347
public class _146 {
358

369
public class Solution1 {
@@ -71,6 +44,8 @@ public class Solution2 {
7144
public class LRUCache {
7245
/**
7346
* The more verbose solution is to implement a doubly linked list yourself plus a map, i.e. LinkedHashMap.
47+
* It's very straightforward to implement this, key notes here: https://docs.google.com/spreadsheets/d/1anN6L5OLhUFd1ANtqDdYY6tz2Ao2H1GESfpDiCfeWyM/edit#gid=0
48+
* (search for the URL of this problem to find it.)
7449
*/
7550
private class Node {
7651
int key;
@@ -110,11 +85,10 @@ public LRUCache(int capacity) {
11085

11186
public int get(int key) {
11287
LRUCache.Node node = map.get(key);
113-
// HashMap allows value to be null, this is superior than HashTable!
88+
// HashMap allows value to be null, this is superior to HashTable!
11489
if (node == null) {
11590
return -1;
11691
} else {
117-
11892
/**Do two operations: this makes the process more clear:
11993
* remove the old node first, and then
12094
* just add the node again.

0 commit comments

Comments
 (0)