-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution146.java
More file actions
executable file
·79 lines (74 loc) · 2.29 KB
/
Copy pathSolution146.java
File metadata and controls
executable file
·79 lines (74 loc) · 2.29 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
import java.util.*;
class LRUCache {
// 双向链表,内部类
class DLinkedNode {
int key;
int value;
DLinkedNode prev;
DLinkedNode next;
public DLinkedNode() {}
public DLinkedNode(int key, int value) {this.key = key; this.value = value;}
}
private int capacity, size;
private HashMap<Integer, DLinkedNode> index = new HashMap<>();
private DLinkedNode head, tail;
public LRUCache(int capacity) {
this.capacity = capacity;
size = 0;
head = new DLinkedNode(); // 伪头节点
tail = new DLinkedNode(); // 伪尾结点
head.next = tail;
tail.prev = head;
}
public int get(int key) {
if (index.containsKey(key)) {
// 找到该结点在链表中的位置
DLinkedNode temp = index.get(key);
// 删除当前结点
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
// 将当前结点转到头部
temp.next = head.next;
temp.prev = head;
head.next.prev = temp;
head.next = temp;
return temp.value;
}
return -1;
}
public void put(int key, int value) {
DLinkedNode temp;
// 如果满了,则删除最后一个节点
if (size == capacity && !index.containsKey(key)) {
size--;
temp = tail.prev;
temp.prev.next = tail;
tail.prev = temp.prev;
index.remove(temp.key); // 从哈希表中移除该结点
}
if (index.containsKey(key)) {
// 找到该结点在链表中的位置并更新值
temp = index.get(key);
temp.value = value;
// 删除当前结点
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
}
else {
temp = new DLinkedNode(key, value);
size++;
}
// 将当前结点转到头部
temp.next = head.next;
temp.prev = head;
head.next.prev = temp;
head.next = temp;
index.put(key, temp);
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/