-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path146.lru-cache.java
More file actions
95 lines (86 loc) · 2.2 KB
/
146.lru-cache.java
File metadata and controls
95 lines (86 loc) · 2.2 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
/*
* @lc app=leetcode id=146 lang=java
*
* [146] LRU Cache
*/
// @lc code=start
/*
HashMap + Doubly LinkedList<Node<key,val>>
time: O(1)
每次来node的时候,新建的node addtohead,修改的node movetohead,movetohead时先删去原有的list中的node
超过capacity的时候删去链表中的最后一个,并删除map中的对应Entry
*/
class LRUCache {
class Node {
int key;
int val;
Node prev;
Node next;
public Node(int key, int val) {
this.key = key;
this.val = val;
prev = null;
next = null;
}
}
Map<Integer, Node> map;
Node head;
Node tail;
int capacity;
public LRUCache(int capacity) {
map = new HashMap<>();
head = new Node(0, 0);
tail = new Node(0, 0);
head.next = tail;
tail.prev = head;
this.capacity = capacity;
}
public int get(int key) {
if (!map.containsKey(key)) return -1;
Node node = map.get(key);
moveToHead(node);
return node.val;
}
public void put(int key, int value) {
if (map.containsKey(key)) {
Node node = map.get(key);
node.val = value;
moveToHead(node);
} else {
Node node = new Node(key, value);
map.put(key, node);
addToHead(node);
if(map.size() > capacity) {
Node last = popLast();
map.remove(last.key);
}
}
}
private void moveToHead(Node node) {
removeNode(node);
addToHead(node);
}
private void removeNode(Node node) {
node.prev.next = node.next;
node.next.prev = node.prev;
}
private void addToHead(Node node) {
Node nextNode = head.next;
head.next = node;
node.next = nextNode;
nextNode.prev = node;
node.prev = head;
}
private Node popLast() {
Node node = tail.prev;
removeNode(node);
return node;
}
}
/**
* 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);
*/
// @lc code=end