Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b3065b4

Browse files
solves lru cache
1 parent fb52881 commit b3065b4

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

β€ŽREADME.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
| 143 | [Reorder List](https://leetcode.com/problems/reorder-list) | | |
130130
| 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal) | [![Java](assets/java.png)](src/BinaryTreePreOrderTraversal.java) [![Python](assets/python.png)](python/binary_tree_preorder_traversal.py) | |
131131
| 145 | [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal) | [![Java](assets/java.png)](src/BinaryTreePostorderTraversal.java) [![Python](assets/python.png)](python/binary_tree_postorder_traversal.py) | |
132-
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache) | | |
132+
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache) | [![Java](assets/java.png)](src/LRUCache.java) | |
133133
| 147 | [Insertion Sort List](https://leetcode.com/problems/insertion-sort-list) | | |
134134
| 148 | [Sort List](https://leetcode.com/problems/sort-list) | | |
135135
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation) | | |

β€Žsrc/LRUCache.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// T: O(n)
2+
// S: O(n)
3+
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class LRUCache {
8+
private static class Node {
9+
int key;
10+
int value;
11+
Node next;
12+
Node previous;
13+
14+
Node() { }
15+
Node(int key, int value) {
16+
this.key = key;
17+
this.value = value;
18+
}
19+
}
20+
21+
private final int capacity;
22+
private final Map<Integer, Node> cache = new HashMap<>();
23+
private Node head, tail;
24+
25+
public LRUCache(int capacity) {
26+
this.capacity = capacity;
27+
head = new Node();
28+
tail = new Node();
29+
head.next = tail;
30+
tail.previous = head;
31+
}
32+
33+
public int get(int key) {
34+
Node node = cache.get(key);
35+
if(node == null) {
36+
return -1;
37+
}
38+
moveToHead(node);
39+
return node.value;
40+
}
41+
42+
public void put(int key, int value) {
43+
Node node = cache.get(key);
44+
45+
if(node == null) {
46+
Node newNode = new Node(key, value);
47+
this.cache.put(key, newNode);
48+
this.addNode(newNode);
49+
50+
if(cache.size() > capacity){
51+
Node tail = popTail();
52+
this.cache.remove(tail.key);
53+
}
54+
} else {
55+
node.value = value;
56+
moveToHead(node);
57+
}
58+
}
59+
60+
private Node popTail(){
61+
Node res = tail.previous;
62+
this.removeNode(res);
63+
return res;
64+
}
65+
66+
private void addNode(Node node) {
67+
node.previous = head;
68+
node.next = head.next;
69+
head.next.previous = node;
70+
head.next = node;
71+
}
72+
73+
private void removeNode(Node node){
74+
Node pre = node.previous;
75+
Node post = node.next;
76+
pre.next = post;
77+
post.previous = pre;
78+
}
79+
80+
private void moveToHead(Node node){
81+
this.removeNode(node);
82+
this.addNode(node);
83+
}
84+
}

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /