forked from yuzhangcmu/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRUCache.java
executable file
·166 lines (128 loc) · 3.84 KB
/
LRUCache.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package Algorithms;
import java.util.HashMap;
public class LRUCache {
public class Node {
Node next;
Node pre;
int key;
int value;
public Node(int key, int value) {
super();
this.next = null;
this.pre = null;
this.key = key;
this.value = value;
}
}
int size;
Node head;
Node tail;
HashMap<Integer, Node> map;
public static void main(String[] strs) {
LRUCache cache = new LRUCache(2);
cache.add(1, 2);
System.out.println("Begine the test");
Integer node1 = cache.get(1);
if (node1 == null) {
System.out.println("null");
} else {
System.out.println(node1);
}
cache.add(1, 3);
node1 = cache.get(1);
if (node1 == null) {
System.out.println("null");
} else {
System.out.println(node1);
}
cache.add(2, 23);
node1 = cache.get(2);
if (node1 == null) {
System.out.println("null");
} else {
System.out.println(node1);
}
cache.add(4, 4);
node1 = cache.get(4);
if (node1 == null) {
System.out.println("null");
} else {
System.out.println(node1);
}
//cache.add(4, 23);
node1 = cache.get(2);
if (node1 == null) {
System.out.println("null");
} else {
System.out.println(node1);
}
}
public LRUCache(int size) {
super();
this.size = size;
this.head = new Node(-1, -1);
this.tail = new Node(-1, -1);
// connect the tail and head;
this.head.next = this.tail;
this.tail.pre = this.head;
this.map = new HashMap<Integer, Node>();
}
// Delete.
public void deleteNode(int key) {
if (map.containsKey(key)) {
removeNode(key);
// Also remove from the map.
map.remove(key);
}
}
public void removeNode(int key) {
if (map.containsKey(key)) {
// remove the node.
Node node = map.get(key);
// remove the node from the list;
node.pre.next = node.next;
node.next.pre = node.pre;
// remove the node from the map.
}
}
public void addNodeToTail(Node node) {
// connect the node.
node.pre = tail.pre;
node.next = tail;
// connect the exit ones to the node.
tail.pre.next = node;
tail.pre = node;
}
public void add(int key, int value) {
Node node = null;
if (map.containsKey(key)) {
node = map.get(key);
// refresh the node value.
node.value = value;
removeNode(key);
} else {
node = new Node(key, value);
map.put(key, node);
}
//System.out.println("the size is : " + map.size());
addNodeToTail(node);
if (map.size() > size && size >= 1) {
int keyRemove = head.next.key;
//System.out.println("the keyRemove is : " + keyRemove);
head.next = head.next.next;
head.next.pre = head;
map.remove(keyRemove);
}
}
public Integer get(int key) {
Node node = null;
if (map.containsKey(key)) {
node = map.get(key);
removeNode(key);
addNodeToTail(node);
return node.value;
} else {
return null;
}
}
}