-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLRUCache.java
More file actions
102 lines (90 loc) · 2.29 KB
/
LRUCache.java
File metadata and controls
102 lines (90 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package leetcode;
import java.util.HashMap;
import java.util.Map;
/**
* 146. LRU Cache
*
* Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
*
* get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
* put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
*
* The cache is initialized with a positive capacity.
*
* Follow up:
* Could you do both operations in O(1) time complexity?
*
*/
public class LRUCache {
private Map<Integer, Node> map;
private Node head;
private Node tail;
private int size;
public LRUCache(int capacity) {
size = capacity;
map = new HashMap<>(capacity);
head = new Node();
tail = new Node();
head.next = tail;
tail.prev = head;
}
public int get(int key) {
Node node = map.get(key);
if (node == null)
{
return -1;
}
removeNode(node);
addNode(node);
return node.val;
}
public void put(int key, int value) {
if (map.containsKey(key))
{
Node node = map.get(key);
node.val = value;
removeNode(node);
addNode(node);
return;
}
Node node = new Node();
node.key = key;
node.val = value;
if (map.size() == size)
{
remove(tail.prev);
add(node);
}
else{
add(node);
}
}
public void remove(Node node)
{
map.remove(node.key);
removeNode(node);
}
public void removeNode(Node node)
{
node.prev.next = node.next;
node.next.prev = node.prev;
}
public void add(Node node)
{
map.put(node.key, node);
addNode(node);
}
public void addNode(Node node)
{
node.next = head.next;
node.prev = head;
head.next.prev = node;
head.next = node;
}
public class Node {
public int val;
public int key;
public Node next;
public Node prev;
}
}