题目链接
法一(LinkedHashMap)
public class Solution146_1 {
private int capacity;
private LinkedHashMap<Integer, Integer> cache;
public Solution146_1(int capacity) {
this.capacity = capacity;
cache = new LinkedHashMap<>(capacity, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry eldest) {
return cache.size() > capacity;
}
};
}
public int get(int key) {
return cache.getOrDefault(key, -1);
}
public void put(int key, int value) {
cache.put(key, value);
}
}
- 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
法二(双向链表 + HashMap)
public class Solution146_2 {
private int capacity;
private int dulSize;
private DulNode dulHead;
private Map<Integer, DulNode> mp = new HashMap<>();
private static class DulNode {
private int key, val;
private DulNode prev, next;
private DulNode() {
}
private DulNode(int key, int val) {
this.key = key;
this.val = val;
}
private DulNode remove() {
prev.next = next;
next.prev = prev;
next = null;
prev = null;
return this;
}
private void insert(DulNode dulNode) {
dulNode.next = next;
dulNode.prev = this;
next.prev = dulNode;
next = dulNode;
}
}
public Solution146_2(int capacity) {
this.capacity = capacity;
dulHead = new DulNode();
dulHead.next = dulHead;
dulHead.prev = dulHead;
}
public int get(int key) {
DulNode dulNode = mp.get(key);
if (dulNode == null) {
return -1;
}
dulNode = dulNode.remove();
dulHead.insert(dulNode);
return dulNode.val;
}
public void put(int key, int value) {
DulNode dulNode = mp.get(key);
if (dulNode == null) {
dulNode = new DulNode(key, value);
mp.put(key, dulNode);
dulSize++;
} else {
dulNode = dulNode.remove();
dulNode.val = value;
}
dulHead.insert(dulNode);
if (dulSize > capacity) {
DulNode dulRemoved = dulHead.prev.remove();
mp.remove(dulRemoved.key);
dulSize--;
}
}
}
- 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
本地测试
lay.showTitle(146);
Solution146_1 sol146_1 = new Solution146_1(2);
sol146_1.put(1, 1);
sol146_1.put(2, 2);
System.out.print(sol146_1.get(1) + " ");
sol146_1.put(3, 3);
System.out.print(sol146_1.get(2) + " ");
sol146_1.put(4, 4);
System.out.print(sol146_1.get(1) + " ");
System.out.print(sol146_1.get(3) + " ");
System.out.println(sol146_1.get(4) + " ");
Solution146_2 sol146_2 = new Solution146_2(2);
sol146_2.put(1, 1);
sol146_2.put(2, 2);
System.out.print(sol146_2.get(1) + " ");
sol146_2.put(3, 3);
System.out.print(sol146_2.get(2) + " ");
sol146_2.put(4, 4);
System.out.print(sol146_2.get(1) + " ");
System.out.print(sol146_2.get(3) + " ");
System.out.println(sol146_2.get(4) + " ");
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24