Skip to content

Commit 2ecb9e4

Browse files
author
lucifer
committed
fix: remove $146
1 parent 77f8caf commit 2ecb9e4

File tree

3 files changed

+12
-53
lines changed

3 files changed

+12
-53
lines changed

README.en.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ The data structures mainly includes:
229229
- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md)
230230
- [0128.longest-consecutive-sequence](./problems/128.longest-consecutive-sequence.md)
231231
- [0145.binary-tree-postorder-traversal](./problems/145.binary-tree-postorder-traversal.md)
232-
- [0146.lru-cache](./problems/146.lru-cache.md)
233232
- [0239.sliding-window-maximum](./problems/239.sliding-window-maximum.md)
234233
- [0295.find-median-from-data-stream](./problems/295.find-median-from-data-stream.md) 🆕
235234
- [0301.remove-invalid-parentheses](./problems/301.remove-invalid-parentheses.md)

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ leetcode 题解,记录自己的 leetcode 解题之路。
261261
- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md)
262262
- [0128.longest-consecutive-sequence](./problems/128.longest-consecutive-sequence.md)
263263
- [0145.binary-tree-postorder-traversal](./problems/145.binary-tree-postorder-traversal.md)
264-
- [0146.lru-cache](./problems/146.lru-cache.md)
265264
- [0212.word-search-ii](./problems/212.word-search-ii.md) 🆕
266265
- [0239.sliding-window-maximum](./problems/239.sliding-window-maximum.md)
267266
- [0295.find-median-from-data-stream](./problems/295.find-median-from-data-stream.md) 🆕
@@ -333,6 +332,10 @@ anki - 文件 - 导入 - 下拉格式选择“打包的 anki 集合”,然后
333332

334333
- 滑动窗口(思路 + 模板)#3 #76 #209 #438 #862 #904 #930 #992 #1004 #1234 #1248
335334

335+
- 动态规划完善。最长递增子序列,最长回文子序列,编辑距离等“字符串”题目, 扔鸡蛋问题。 解题模板,滚动数组。
336+
337+
- 堆可以解决的题目。 手写堆
338+
336339
## 关注我
337340

338341
我重新整理了下自己的公众号,并且我还给它换了一个名字`脑洞前端`,它是一个帮助你打开大前端新世界大门的钥匙 🔑,在这里你可以听到新奇的观点,看到一些技术尝新,还会收到系统性总结和思考。

problems/146.lru-cache.md

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,68 +30,24 @@ cache.get(4); // returns 4
3030

3131
## 思路
3232

33-
由于是保留是最近使用的N条数据,这就和队列的特性很符合, 先进入队列的,先出队列。
33+
`本题已被收录到我的新书中,敬请期待~`
3434

35-
因此思路就是用一个队列来记录目前缓存的所有key, 每次push都进行判断,如果
36-
超出最大容量限制则进行清除缓存的操作, 具体清除谁就按照刚才说的队列方式进行处理,同时对key进行入队操作。
35+
由于是保留是最近使用的 N 条数据,这就和队列的特性很符合, 先进入队列的,先出队列。
3736

38-
get的时候,如果缓存中有,则调整队列(具体操作为删除指定元素和入队两个操作)。 缓存中没有则返回-1
37+
因此思路就是用一个队列来记录目前缓存的所有 key, 每次 push 都进行判断,如果
38+
超出最大容量限制则进行清除缓存的操作, 具体清除谁就按照刚才说的队列方式进行处理,同时对 key 进行入队操作。
39+
40+
get 的时候,如果缓存中有,则调整队列(具体操作为删除指定元素和入队两个操作)。 缓存中没有则返回-1
3941

4042
## 关键点解析
4143

4244
- 队列简化操作
4345

4446
- 队列的操作是这道题的灵魂, 很容易少考虑情况
4547

46-
4748
## 代码
4849

4950
```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-
*/
9551
/**
9652
* @param {number} capacity
9753
*/
@@ -151,5 +107,6 @@ LRUCache.prototype.put = function(key, value) {
151107
* obj.put(key,value)
152108
*/
153109

154-
155110
```
111+
112+
本题删除的时间复杂度是不符合要求的。 应该采用双向链表在 O(1) 时间找到前驱进行删除。

0 commit comments

Comments
 (0)