-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRU 缓存 [OrIXps].html
54 lines (41 loc) · 2.36 KB
/
LRU 缓存 [OrIXps].html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<div class="title__3Vvk">
<p>运用所掌握的数据结构,设计和实现一个 <a href="https://baike.baidu.com/item/LRU" target="_blank">LRU (Least Recently Used,最近最少使用) 缓存机制</a> 。</p>
<p>实现 <code>LRUCache</code> 类:</p>
<ul>
<li><code>LRUCache(int capacity)</code> 以正整数作为容量 <code>capacity</code> 初始化 LRU 缓存</li>
<li><code>int get(int key)</code> 如果关键字 <code>key</code> 存在于缓存中,则返回关键字的值,否则返回 <code>-1</code> 。</li>
<li><code>void put(int key, int value)</code> 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。</li>
</ul>
<p> </p>
<p><strong>示例:</strong></p>
<pre>
<strong>输入</strong>
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
<strong>输出</strong>
[null, null, null, 1, null, -1, null, -1, 3, 4]
<strong>解释</strong>
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
lRUCache.get(1); // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
lRUCache.get(2); // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
lRUCache.get(1); // 返回 -1 (未找到)
lRUCache.get(3); // 返回 3
lRUCache.get(4); // 返回 4
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= capacity <= 3000</code></li>
<li><code>0 <= key <= 10000</code></li>
<li><code>0 <= value <= 10<sup>5</sup></code></li>
<li>最多调用 <code>2 * 10<sup>5</sup></code> 次 <code>get</code> 和 <code>put</code></li>
</ul>
</div>
<p> </p>
<p><strong>进阶</strong>:是否可以在 <code>O(1)</code> 时间复杂度内完成这两种操作?</p>
<p> </p>
<p><meta charset="UTF-8" />注意:本题与主站 146 题相同:<a href="https://leetcode-cn.com/problems/lru-cache/">https://leetcode-cn.com/problems/lru-cache/</a> </p>