-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRU Cache.java
65 lines (59 loc) · 1.95 KB
/
LRU Cache.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
// this question combine doublely linked list with hashmap
// how to design the composition in the class and how to insert and delete linked list node should
// be the most important basic thing in this problem
public class LRUCache {
private class Node{
int key;
int value;
Node prev;// 前节点
Node next;// 后节点,便于删除使用
public Node(int key, int value){
this.key = key;
this.value = value;
this.prev = null;// initialization
this.next = null;
}
}
private int capacity;
private HashMap<Integer, Node> LRU = new HashMap<Integer, Node>();
private Node head = new Node(-1, -1);
private Node tail = new Node(-1, -1);
public LRUCache(int capacity) {
this.capacity = capacity;
head.next = tail;
tail.prev = head;
}
// get the Node of specific key and move it to the tail
public int get(int key) {
if(!LRU.containsKey(key)){
return -1;
}
Node curr = LRU.get(key);
curr.prev.next = curr.next; // doublely linked list
curr.next.prev = curr.prev;
// move curr to tail
move_to_tail(curr);
return LRU.get(key).value;
}
public void set(int key, int value) {
// the node exists
if(get(key) != -1){
LRU.get(key).value = value;
return;
}
if(LRU.size() == capacity){
LRU.remove(head.next.key); // remove the first element in the hashmap
head.next = head.next.next;// move head to the second element
head.next.prev = head; // prev of second ele points to head
}
Node curr = new Node(key, value);
LRU.put(key, curr);
move_to_tail(curr);
}
public void move_to_tail(Node node){
node.prev = tail.prev;
tail.prev = node;
node.prev.next = node;
node.next = tail;
}
}