-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRUCache.java
53 lines (42 loc) · 869 Bytes
/
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
public class Cache {
private int capacity;
private HashMap<Integer, Node> map;
Node head;
Node end;
public Cache(int capacity) {
map = new HashMap<Integer, Node>();
this.capacity = capacity;
head = end = null;
}
public int get(int key) {
if (map.containsKey(key)) {
Node node = map.get(key);
remove(node);
setHead(node);
return node.value;
}
return -1;
}
public void set(int key, int value) {
if (head == null) {
head = tail = new Node(key, value);
map.put(key, head);
}else {
}
}
private void remove(Node n) {
}
private void setHead(Node n) {
}
private static class Node {
int key;
int value;
Node prev;
Node next;
public Node(int key, int value) {
this.key = key;
this.value = value;
prev = next = null;
}
}
}