|
24 | 24 | */ |
25 | 25 | class LRUCache { |
26 | 26 |
|
27 | | - private final int capacity; |
28 | | - private final Map<Integer, Node> cache; |
29 | | - private final Node head; |
30 | | - private final Node tail; |
| 27 | + private final int capacity; |
| 28 | + private final Map<Integer, Node> cache; |
| 29 | + private final Node head; |
| 30 | + private final Node tail; |
31 | 31 |
|
32 | | - /** |
33 | | - * Remember capacity. |
34 | | - * Create cache map and doubly linked list. |
35 | | - */ |
36 | | - public LRUCache(int capacity) { |
37 | | - this.capacity = capacity; |
38 | | - cache = new HashMap<>(); |
39 | | - head = new Node(); |
40 | | - tail = new Node(); |
41 | | - head.next = tail; |
42 | | - tail.prev = head; |
43 | | - } |
| 32 | + /** |
| 33 | + * Remember capacity. |
| 34 | + * Create cache map and doubly linked list. |
| 35 | + */ |
| 36 | + public LRUCache(int capacity) { |
| 37 | + this.capacity = capacity; |
| 38 | + cache = new HashMap<>(); |
| 39 | + head = new Node(); |
| 40 | + tail = new Node(); |
| 41 | + head.next = tail; |
| 42 | + tail.prev = head; |
| 43 | + } |
44 | 44 |
|
45 | | - /** |
46 | | - * Check key in cache. |
47 | | - * If not in cache, return -1. |
48 | | - * If in cache, get the node, move it to head, return its value. |
49 | | - */ |
50 | | - public int get(int key) { |
51 | | - if (!cache.containsKey(key)) { |
52 | | - return -1; |
53 | | - } |
54 | | - Node node = cache.get(key); |
55 | | - moveToHead(node); |
56 | | - return node.val; |
| 45 | + /** |
| 46 | + * Check key in cache. |
| 47 | + * If not in cache, return -1. |
| 48 | + * If in cache, get the node, move it to head, return its value. |
| 49 | + */ |
| 50 | + public int get(int key) { |
| 51 | + if (!cache.containsKey(key)) { |
| 52 | + return -1; |
57 | 53 | } |
| 54 | + Node node = cache.get(key); |
| 55 | + moveToHead(node); |
| 56 | + return node.val; |
| 57 | + } |
58 | 58 |
|
59 | | - /** |
60 | | - * If key already in cache, only need to update value: |
61 | | - * | Get the node, update its value, move to head. |
62 | | - * If key is not in cache: |
63 | | - * | Create a new node. |
64 | | - * | Add it to the head of list and put it in cache. |
65 | | - * | If cache size exceeds capacity: |
66 | | - * | Get the last node, which is the previous node of tail. |
67 | | - * | Remove it from list by its self-reference. |
68 | | - * | Remove it from cache by its key. |
69 | | - */ |
70 | | - public void set(int key, int value) { |
71 | | - if (cache.containsKey(key)) { |
72 | | - Node node = cache.get(key); |
73 | | - node.val = value; |
74 | | - moveToHead(node); |
75 | | - } else { |
76 | | - Node newNode = new Node(key, value); |
77 | | - addNode(newNode); |
78 | | - cache.put(key, newNode); |
79 | | - if (cache.size() > capacity) { |
80 | | - Node last = tail.prev; |
81 | | - removeNode(last); |
82 | | - cache.remove(last.key); |
83 | | - } |
84 | | - } |
| 59 | + /** |
| 60 | + * If key already in cache, only need to update value: |
| 61 | + * | Get the node, update its value, move to head. |
| 62 | + * If key is not in cache: |
| 63 | + * | Create a new node. |
| 64 | + * | Add it to the head of list and put it in cache. |
| 65 | + * | If cache size exceeds capacity: |
| 66 | + * | Get the last node, which is the previous node of tail. |
| 67 | + * | Remove it from list by its self-reference. |
| 68 | + * | Remove it from cache by its key. |
| 69 | + */ |
| 70 | + public void set(int key, int value) { |
| 71 | + if (cache.containsKey(key)) { |
| 72 | + Node node = cache.get(key); |
| 73 | + node.val = value; |
| 74 | + moveToHead(node); |
| 75 | + } else { |
| 76 | + Node newNode = new Node(key, value); |
| 77 | + addNode(newNode); |
| 78 | + cache.put(key, newNode); |
| 79 | + if (cache.size() > capacity) { |
| 80 | + Node last = tail.prev; |
| 81 | + removeNode(last); |
| 82 | + cache.remove(last.key); |
| 83 | + } |
85 | 84 | } |
| 85 | + } |
86 | 86 |
|
87 | | - /** |
88 | | - * Remove node from list and add it to head. |
89 | | - */ |
90 | | - private void moveToHead(Node node) { |
91 | | - removeNode(node); |
92 | | - addNode(node); |
93 | | - } |
| 87 | + /** |
| 88 | + * Remove node from list and add it to head. |
| 89 | + */ |
| 90 | + private void moveToHead(Node node) { |
| 91 | + removeNode(node); |
| 92 | + addNode(node); |
| 93 | + } |
94 | 94 |
|
95 | | - /** |
96 | | - * Remove a node from double linked list. |
97 | | - */ |
98 | | - private void removeNode(Node node) { |
99 | | - node.prev.next = node.next; |
100 | | - node.next.prev = node.prev; |
101 | | - } |
| 95 | + /** |
| 96 | + * Remove a node from double linked list. |
| 97 | + */ |
| 98 | + private void removeNode(Node node) { |
| 99 | + node.prev.next = node.next; |
| 100 | + node.next.prev = node.prev; |
| 101 | + } |
102 | 102 |
|
103 | | - /** |
104 | | - * Add a node after head. |
105 | | - */ |
106 | | - private void addNode(Node node) { |
107 | | - node.prev = head; |
108 | | - node.next = head.next; |
109 | | - head.next.prev = node; |
110 | | - head.next = node; |
111 | | - } |
| 103 | + /** |
| 104 | + * Add a node after head. |
| 105 | + */ |
| 106 | + private void addNode(Node node) { |
| 107 | + node.prev = head; |
| 108 | + node.next = head.next; |
| 109 | + head.next.prev = node; |
| 110 | + head.next = node; |
| 111 | + } |
112 | 112 |
|
113 | | - /** |
114 | | - * Double linked list node. |
115 | | - * With previous node, next node, key, and value. |
116 | | - */ |
117 | | - class Node { |
| 113 | + /** |
| 114 | + * Double linked list node. |
| 115 | + * With previous node, next node, key, and value. |
| 116 | + */ |
| 117 | + class Node { |
118 | 118 |
|
119 | | - Node prev; |
120 | | - Node next; |
121 | | - int key; |
122 | | - int val; |
| 119 | + Node prev; |
| 120 | + Node next; |
| 121 | + int key; |
| 122 | + int val; |
123 | 123 |
|
124 | | - public Node() { |
125 | | - } |
| 124 | + public Node() { |
| 125 | + } |
126 | 126 |
|
127 | | - public Node(int key, int val) { |
128 | | - this.key = key; |
129 | | - this.val = val; |
130 | | - } |
| 127 | + public Node(int key, int val) { |
| 128 | + this.key = key; |
| 129 | + this.val = val; |
131 | 130 | } |
| 131 | + } |
132 | 132 | } |
0 commit comments