-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBM100.java
More file actions
85 lines (79 loc) · 2.05 KB
/
BM100.java
File metadata and controls
85 lines (79 loc) · 2.05 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
package NiukeTOP101;
import java.util.HashMap;
import java.util.Map;
public class BM100 {
private int capacity;
private Map<Integer, Node> map;
private Node head, tail;
private int used;
// 设置双向链表结构
static class Node{
int key;
int val;
Node pre;
Node next;
// 初始化
public Node(int key, int val){
this.key = key;
this.val = val;
this.pre = null;
this.next = null;
}
}
public BM100(int capacity) {
this.capacity = capacity;
this.map = new HashMap<>();
this.used = 0;
}
public int get(int key) {
if(!map.containsKey(key)){
return -1;
}
makeRecently(key);
return map.get(key).val;
}
// 把 key 对应的结点移到链表头部
private void makeRecently(int key) {
Node t = map.get(key);
if(t != head){
if(t == tail){
tail = tail.pre;
tail.next = null;
}else{
t.pre.next = t.next;
t.next.pre = t.pre;
}
t.pre = null;
t.next = head;
head.pre = t;
head = t;
}
}
public void set(int key, int value) {
// 如果 key 已存在,直接修改值,再移到链表头部
if(map.containsKey(key)){
map.get(key).val = value;
makeRecently(key);
return;
}
// 如果达到容量上限,就要移除尾部结点,注意HashMap要remove
if(used == capacity){
map.remove(tail.key);
tail = tail.pre;
tail.next = null;
used--;
}
// 头结点为空,单独处理
if(head == null){
head = new Node(key, value);
tail = head;
}else{
Node t = new Node(key, value);
t.next = head;
head.pre = t;
head = t;
}
map.put(key, head);
used++;
}
}