|
|
@ -3,7 +3,7 @@ package 第03期.mca_05;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
|
|
|
|
// 测试链接 : https://leetcode.cn/problems/lru-cache/
|
|
|
|
// 测试链接 : https://leetcode.cn/problems/lru-cache/
|
|
|
|
public class Code08_LRUCache {
|
|
|
|
public class Code05_LRUCache {
|
|
|
|
|
|
|
|
|
|
|
|
// 提交以下这个类
|
|
|
|
// 提交以下这个类
|
|
|
|
public class LRUCache {
|
|
|
|
public class LRUCache {
|
|
|
@ -43,28 +43,28 @@ public class Code08_LRUCache {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void moveNodeToTail(Node node) {
|
|
|
|
public void moveNodeToTail(Node x) {
|
|
|
|
if (tail == node) {
|
|
|
|
if (tail == x) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (head == node) {
|
|
|
|
if (head == x) { // x在头部
|
|
|
|
head = node.next;
|
|
|
|
head = x.next;
|
|
|
|
head.last = null;
|
|
|
|
head.last = null;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
node.last.next = node.next;
|
|
|
|
x.last.next = x.next;
|
|
|
|
node.next.last = node.last;
|
|
|
|
x.next.last = x.last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node.last = tail;
|
|
|
|
x.last = tail;
|
|
|
|
node.next = null;
|
|
|
|
x.next = null;
|
|
|
|
this.tail.next = node;
|
|
|
|
tail.next = x;
|
|
|
|
this.tail = node;
|
|
|
|
tail = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Node removeHead() {
|
|
|
|
public Node removeHead() {
|
|
|
|
if (head == null) {
|
|
|
|
if (head == null) {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Node ans = this.head;
|
|
|
|
Node ans = head;
|
|
|
|
if (head == tail) {
|
|
|
|
if (head == tail) {
|
|
|
|
head = null;
|
|
|
|
head = null;
|
|
|
|
tail = null;
|
|
|
|
tail = null;
|
|
|
@ -78,8 +78,11 @@ public class Code08_LRUCache {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// A -> Node
|
|
|
|
private HashMap<Integer, Node> keyNodeMap;
|
|
|
|
private HashMap<Integer, Node> keyNodeMap;
|
|
|
|
|
|
|
|
// Node 串成的双向链表
|
|
|
|
private DoubleLinkedList nodeList;
|
|
|
|
private DoubleLinkedList nodeList;
|
|
|
|
|
|
|
|
// 这个结构的容量!
|
|
|
|
private final int capacity;
|
|
|
|
private final int capacity;
|
|
|
|
|
|
|
|
|
|
|
|
public LRUCache(int cap) {
|
|
|
|
public LRUCache(int cap) {
|
|
|
@ -97,12 +100,16 @@ public class Code08_LRUCache {
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// A 23
|
|
|
|
public void put(int key, int value) {
|
|
|
|
public void put(int key, int value) {
|
|
|
|
if (keyNodeMap.containsKey(key)) {
|
|
|
|
if (keyNodeMap.containsKey(key)) {
|
|
|
|
Node node = keyNodeMap.get(key);
|
|
|
|
Node node = keyNodeMap.get(key);
|
|
|
|
node.val = value;
|
|
|
|
node.val = value;
|
|
|
|
nodeList.moveNodeToTail(node);
|
|
|
|
nodeList.moveNodeToTail(node);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 之前没有A的记录,新增!
|
|
|
|
|
|
|
|
// 1) 结构满了
|
|
|
|
|
|
|
|
// 2) 结构没有满
|
|
|
|
if (keyNodeMap.size() == capacity) {
|
|
|
|
if (keyNodeMap.size() == capacity) {
|
|
|
|
removeMostUnusedCache();
|
|
|
|
removeMostUnusedCache();
|
|
|
|
}
|
|
|
|
}
|