Skip to content

Commit 5aef01b

Browse files
author
luzhipeng
committed
lru
1 parent 2a2af41 commit 5aef01b

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

Diff for: 146.lru-cache.md

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
## 题目地址
2+
https://leetcode.com/problems/lru-cache/description/
3+
4+
## 题目描述
5+
6+
```
7+
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
8+
9+
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
10+
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.
11+
12+
Follow up:
13+
Could you do both operations in O(1) time complexity?
14+
15+
Example:
16+
17+
LRUCache cache = new LRUCache( 2 /* capacity */ );
18+
19+
cache.put(1, 1);
20+
cache.put(2, 2);
21+
cache.get(1); // returns 1
22+
cache.put(3, 3); // evicts key 2
23+
cache.get(2); // returns -1 (not found)
24+
cache.put(4, 4); // evicts key 1
25+
cache.get(1); // returns -1 (not found)
26+
cache.get(3); // returns 3
27+
cache.get(4); // returns 4
28+
29+
```
30+
31+
## 思路
32+
33+
由于是保留是最近使用的N条数据,这就和队列的特性很符合, 先进入队列的,先出队列。
34+
35+
因此思路就是用一个队列来记录目前缓存的所有key, 每次push都进行判断,如果
36+
超出最大容量限制则进行清除缓存的操作, 具体清除谁就按照刚才说的队列方式进行处理,同时对key进行入队操作。
37+
38+
get的时候,如果缓存中有,则调整队列(具体操作为删除指定元素和入队两个操作)。 缓存中没有则返回-1
39+
40+
## 关键点解析
41+
42+
- 队列简化操作
43+
44+
- 队列的操作是这道题的灵魂, 很容易少考虑情况
45+
46+
47+
## 代码
48+
49+
```js
50+
/*
51+
* @lc app=leetcode id=146 lang=javascript
52+
*
53+
* [146] LRU Cache
54+
*
55+
* https://leetcode.com/problems/lru-cache/description/
56+
*
57+
* algorithms
58+
* Hard (24.17%)
59+
* Total Accepted: 272.8K
60+
* Total Submissions: 1.1M
61+
* Testcase Example: '["LRUCache","put","put","get","put","get","put","get","get","get"]\n[[2],[1,1],[2,2],[1],[3,3],[2],[4,4],[1],[3],[4]]'
62+
*
63+
*
64+
* Design and implement a data structure for Least Recently Used (LRU) cache.
65+
* It should support the following operations: get and put.
66+
*
67+
*
68+
*
69+
* get(key) - Get the value (will always be positive) of the key if the key
70+
* exists in the cache, otherwise return -1.
71+
* put(key, value) - Set or insert the value if the key is not already present.
72+
* When the cache reached its capacity, it should invalidate the least recently
73+
* used item before inserting a new item.
74+
*
75+
*
76+
* Follow up:
77+
* Could you do both operations in O(1) time complexity?
78+
*
79+
* Example:
80+
*
81+
* LRUCache cache = new LRUCache( 2 );
82+
*
83+
* cache.put(1, 1);
84+
* cache.put(2, 2);
85+
* cache.get(1); // returns 1
86+
* cache.put(3, 3); // evicts key 2
87+
* cache.get(2); // returns -1 (not found)
88+
* cache.put(4, 4); // evicts key 1
89+
* cache.get(1); // returns -1 (not found)
90+
* cache.get(3); // returns 3
91+
* cache.get(4); // returns 4
92+
*
93+
*
94+
*/
95+
/**
96+
* @param {number} capacity
97+
*/
98+
var LRUCache = function(capacity) {
99+
this.cache = {};
100+
this.capacity = capacity;
101+
this.size = 0;
102+
this.queue = [];
103+
};
104+
105+
/**
106+
* @param {number} key
107+
* @return {number}
108+
*/
109+
LRUCache.prototype.get = function(key) {
110+
const hit = this.cache[key];
111+
112+
if (hit !== undefined) {
113+
this.queue = this.queue.filter(q => q !== key);
114+
this.queue.push(key);
115+
return hit;
116+
}
117+
return -1;
118+
};
119+
120+
/**
121+
* @param {number} key
122+
* @param {number} value
123+
* @return {void}
124+
*/
125+
LRUCache.prototype.put = function(key, value) {
126+
const hit = this.cache[key];
127+
128+
// update cache
129+
this.cache[key] = value;
130+
131+
if (!hit) {
132+
// invalid cache and resize size;
133+
if (this.size === this.capacity) {
134+
// invalid cache
135+
const key = this.queue.shift();
136+
this.cache[key] = undefined;
137+
} else {
138+
this.size = this.size + 1;
139+
}
140+
this.queue.push(key);
141+
} else {
142+
this.queue = this.queue.filter(q => q !== key);
143+
this.queue.push(key);
144+
}
145+
};
146+
147+
/**
148+
* Your LRUCache object will be instantiated and called as such:
149+
* var obj = new LRUCache(capacity)
150+
* var param_1 = obj.get(key)
151+
* obj.put(key,value)
152+
*/
153+
154+
155+
```

0 commit comments

Comments
 (0)