1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-12-17 17:52:34 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2022-03-29 16:56:27 +08:00
parent e730aa6794
commit ad15da05aa
2517 changed files with 7358 additions and 7332 deletions

View File

@@ -12,7 +12,7 @@
"translatedContent": "<div class=\"title__3Vvk\">\n<p>运用所掌握的数据结构,设计和实现一个&nbsp; <a href=\"https://baike.baidu.com/item/LRU\" target=\"_blank\">LRU (Least Recently Used最近最少使用) 缓存机制</a> 。</p>\n\n<p>实现 <code>LRUCache</code> 类:</p>\n\n<ul>\n\t<li><code>LRUCache(int capacity)</code> 以正整数作为容量&nbsp;<code>capacity</code> 初始化 LRU 缓存</li>\n\t<li><code>int get(int key)</code> 如果关键字 <code>key</code> 存在于缓存中,则返回关键字的值,否则返回 <code>-1</code> 。</li>\n\t<li><code>void put(int key, int value)</code>&nbsp;如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。</li>\n</ul>\n\n<p>&nbsp;</p>\n\n<p><strong>示例:</strong></p>\n\n<pre>\n<strong>输入</strong>\n[&quot;LRUCache&quot;, &quot;put&quot;, &quot;put&quot;, &quot;get&quot;, &quot;put&quot;, &quot;get&quot;, &quot;put&quot;, &quot;get&quot;, &quot;get&quot;, &quot;get&quot;]\n[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]\n<strong>输出</strong>\n[null, null, null, 1, null, -1, null, -1, 3, 4]\n\n<strong>解释</strong>\nLRUCache lRUCache = new LRUCache(2);\nlRUCache.put(1, 1); // 缓存是 {1=1}\nlRUCache.put(2, 2); // 缓存是 {1=1, 2=2}\nlRUCache.get(1); // 返回 1\nlRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}\nlRUCache.get(2); // 返回 -1 (未找到)\nlRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}\nlRUCache.get(1); // 返回 -1 (未找到)\nlRUCache.get(3); // 返回 3\nlRUCache.get(4); // 返回 4\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= capacity &lt;= 3000</code></li>\n\t<li><code>0 &lt;= key &lt;= 10000</code></li>\n\t<li><code>0 &lt;= value &lt;= 10<sup>5</sup></code></li>\n\t<li>最多调用 <code>2 * 10<sup>5</sup></code> 次 <code>get</code> 和 <code>put</code></li>\n</ul>\n</div>\n\n<p>&nbsp;</p>\n\n<p><strong>进阶</strong>:是否可以在&nbsp;<code>O(1)</code> 时间复杂度内完成这两种操作?</p>\n\n<p>&nbsp;</p>\n\n<p><meta charset=\"UTF-8\" />注意:本题与主站 146&nbsp;题相同:<a href=\"https://leetcode-cn.com/problems/lru-cache/\">https://leetcode-cn.com/problems/lru-cache/</a>&nbsp;</p>\n",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 39,
"likes": 40,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
@@ -162,7 +162,7 @@
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"9.4K\", \"totalSubmission\": \"17.5K\", \"totalAcceptedRaw\": 9450, \"totalSubmissionRaw\": 17531, \"acRate\": \"53.9%\"}",
"stats": "{\"totalAccepted\": \"9.5K\", \"totalSubmission\": \"17.7K\", \"totalAcceptedRaw\": 9545, \"totalSubmissionRaw\": 17740, \"acRate\": \"53.8%\"}",
"hints": [],
"solution": null,
"status": null,