Skip to content

Commit 259e029

Browse files
update 146
1 parent 910ca97 commit 259e029

File tree

1 file changed

+24
-24
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+24
-24
lines changed

src/main/java/com/fishercoder/solutions/_146.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@
66

77
/**
88
* 146. LRU Cache
9-
*
9+
* <p>
1010
* Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
11-
12-
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
13-
put(key, value) - Set or insert the value if the key is not already present.
14-
When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
15-
16-
Follow up:
17-
Could you do both operations in O(1) time complexity?
18-
19-
Example:
20-
21-
LRUCache cache = new LRUCache(2);//capacity
22-
23-
cache.put(1, 1);
24-
cache.put(2, 2);
25-
cache.get(1); // returns 1
26-
cache.put(3, 3); // evicts key 2
27-
cache.get(2); // returns -1 (not found)
28-
cache.put(4, 4); // evicts key 1
29-
cache.get(1); // returns -1 (not found)
30-
cache.get(3); // returns 3
31-
cache.get(4); // returns 4
32-
*/
11+
* <p>
12+
* get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
13+
* put(key, value) - Set or insert the value if the key is not already present.
14+
* When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
15+
* <p>
16+
* Follow up:
17+
* Could you do both operations in O(1) time complexity?
18+
* <p>
19+
* Example:
20+
* <p>
21+
* LRUCache cache = new LRUCache(2);//capacity
22+
* <p>
23+
* cache.put(1, 1);
24+
* cache.put(2, 2);
25+
* cache.get(1); // returns 1
26+
* cache.put(3, 3); // evicts key 2
27+
* cache.get(2); // returns -1 (not found)
28+
* cache.put(4, 4); // evicts key 1
29+
* cache.get(1); // returns -1 (not found)
30+
* cache.get(3); // returns 3
31+
* cache.get(4); // returns 4
32+
*/
3333

3434
public class _146 {
3535

@@ -70,7 +70,7 @@ public void put(int key, int value) {
7070
public class Solution2 {
7171
public class LRUCache {
7272
/**
73-
* The more verbose solution is to write a doubly linked list plus a map.
73+
* The more verbose solution is to implement a doubly linked list yourself plus a map, i.e. LinkedHashMap.
7474
*/
7575
private class Node {
7676
int key;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy