-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLRUCache.java
More file actions
103 lines (87 loc) · 2.26 KB
/
LRUCache.java
File metadata and controls
103 lines (87 loc) · 2.26 KB
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
package test;
import java.util.HashMap;
import java.util.Map;
public class LRUCache {
private Map<Integer, Node> map;
private DoubleList cache;
private Integer capacity;
public LRUCache(int capacity) {
this.map = new HashMap<>();
this.cache = new DoubleList();
this.capacity = capacity;
}
public int get(int key) {
if(!map.containsKey(key)) {
return -1;
}
int val = map.get(key).value;
put(key, val);
return val;
}
public void put(int key, int value) {
Node node = new Node(key, value);
if (map.containsKey(key)) {
cache.remove(map.get(key));
cache.addFirst(node);
map.put(key, node);
} else {
if (capacity == cache.size()) {
Node last = cache.removeLast();
map.remove(last.key);
}
cache.addFirst(node);
map.put(key, node);
}
}
}
class Node {
public int key, value;
public Node pre, next;
public Node(int key, int value) {
this.key = key;
this.value = value;
}
}
class DoubleList {
private Node head, tail;
private int size;
// 在链表头部添加结点x,时间O(1)
public void addFirst(Node node){
if (head == null) {
head = tail = node;
} else {
Node n = head;
n.pre = node;
node.next = n;
head = node;
}
size ++;
}
// 删除链表中的x结点(x一定存在)
// 由于是双链表且给的是目标Node
public void remove(Node node){
if(head == node && tail == node) {
head = tail = null;
} else if(head == node) {
node.next.pre = null;
head = node.next;
} else if(tail == node) {
tail.pre.next = null;
tail = node.pre;
} else {
node.next.pre = node.pre;
node.pre.next = node.next;
}
size --;
}
// 删除链表中最后一个结点,并返回该结点
public Node removeLast(){
Node node = tail;
remove(tail);
return node;
}
// 返回链表长度
public int size() {
return size;
}
}