@@ -33,7 +33,7 @@ Could you do both operations in O(1) time complexity?
33
33
34
34
public class _146 {
35
35
36
- public class LinkedHashMapSolution {
36
+ public class Solution1 {
37
37
/**
38
38
* The shortest implementation is to use LinkedHashMap:
39
39
* specify a size of the linkedHashMap;
@@ -47,7 +47,7 @@ public class LinkedHashMapSolution {
47
47
private Map <Integer , Integer > cache ;
48
48
private final int max ;
49
49
50
- public LinkedHashMapSolution (int capacity ) {
50
+ public Solution1 (int capacity ) {
51
51
max = capacity ;
52
52
cache = new LinkedHashMap <Integer , Integer >(capacity , 1.0f , true ) {
53
53
public boolean removeEldestEntry (Map .Entry eldest ) {
@@ -65,13 +65,14 @@ public void set(int key, int value) {
65
65
}
66
66
}
67
67
68
- public class DoublyLinkedListPlusHashMapSolution {
68
+ public class Solution2 {
69
+ /**The more verbose solution is to write a doubly linked list plus a map.*/
69
70
private class Node {
70
71
int key ;
71
72
int value ;
72
73
73
- DoublyLinkedListPlusHashMapSolution .Node prev ;
74
- DoublyLinkedListPlusHashMapSolution .Node next ;
74
+ Solution2 .Node prev ;
75
+ Solution2 .Node next ;
75
76
76
77
Node (int k , int v ) {
77
78
this .key = k ;
@@ -86,24 +87,24 @@ private class Node {
86
87
87
88
private int capacity ;
88
89
private int count ;
89
- private DoublyLinkedListPlusHashMapSolution .Node head ;
90
- private DoublyLinkedListPlusHashMapSolution .Node tail ;
91
- private Map <Integer , DoublyLinkedListPlusHashMapSolution .Node > map ;
90
+ private Solution2 .Node head ;
91
+ private Solution2 .Node tail ;
92
+ private Map <Integer , Solution2 .Node > map ;
92
93
// ATTN: the value should be Node type! This is the whole point of having a class called Node!
93
94
94
- public DoublyLinkedListPlusHashMapSolution (int capacity ) {
95
+ public Solution2 (int capacity ) {
95
96
this .capacity = capacity ;
96
97
this .count = 0 ;// we need a count to keep track of the number of elements in the cache so
97
98
// that we know when to evict the LRU one from the cache
98
99
this .map = new HashMap ();
99
- head = new DoublyLinkedListPlusHashMapSolution .Node ();
100
- tail = new DoublyLinkedListPlusHashMapSolution .Node ();
100
+ head = new Solution2 .Node ();
101
+ tail = new Solution2 .Node ();
101
102
head .next = tail ;
102
103
tail .prev = head ;
103
104
}
104
105
105
106
public int get (int key ) {
106
- DoublyLinkedListPlusHashMapSolution .Node node = map .get (key );
107
+ Solution2 .Node node = map .get (key );
107
108
// HashMap allows value to be null, this is superior than HashTable!
108
109
if (node == null ) {
109
110
return -1 ;
@@ -122,9 +123,9 @@ public int get(int key) {
122
123
}
123
124
124
125
public void set (int key , int value ) {
125
- DoublyLinkedListPlusHashMapSolution .Node node = map .get (key );
126
+ Solution2 .Node node = map .get (key );
126
127
if (node == null ) {
127
- node = new DoublyLinkedListPlusHashMapSolution .Node (key , value );
128
+ node = new Solution2 .Node (key , value );
128
129
map .put (key , node );
129
130
add (node );
130
131
count ++;
@@ -133,7 +134,7 @@ public void set(int key, int value) {
133
134
/** ATTN: It's tail.prev, not tail, because tail is always an invalid node, it
134
135
doesn't contain anything, it's always the tail.prev that is the last node in the
135
136
cache*/
136
- DoublyLinkedListPlusHashMapSolution .Node toDelete = tail .prev ;
137
+ Solution2 .Node toDelete = tail .prev ;
137
138
map .remove (toDelete .key );
138
139
remove (toDelete );
139
140
count --;
@@ -145,16 +146,16 @@ public void set(int key, int value) {
145
146
}
146
147
}
147
148
148
- private void remove (DoublyLinkedListPlusHashMapSolution .Node node ) {
149
- DoublyLinkedListPlusHashMapSolution .Node next = node .next ;
150
- DoublyLinkedListPlusHashMapSolution .Node prev = node .prev ;
149
+ private void remove (Solution2 .Node node ) {
150
+ Solution2 .Node next = node .next ;
151
+ Solution2 .Node prev = node .prev ;
151
152
prev .next = next ;
152
153
next .prev = prev ;
153
154
}
154
155
155
- private void add (DoublyLinkedListPlusHashMapSolution .Node node ) {
156
+ private void add (Solution2 .Node node ) {
156
157
// ATTN: we'll always add the node into the first position: head.next!!!!
157
- DoublyLinkedListPlusHashMapSolution .Node next = head .next ;
158
+ Solution2 .Node next = head .next ;
158
159
head .next = node ;
159
160
node .next = next ;
160
161
node .prev = head ;
0 commit comments