mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-13 19:31:42 +08:00
add leetcode problem-cn part2
This commit is contained in:
21
算法题(国内版)/problem (Chinese)/LRU 缓存 [lru-cache-lcci].html
Normal file
21
算法题(国内版)/problem (Chinese)/LRU 缓存 [lru-cache-lcci].html
Normal file
@@ -0,0 +1,21 @@
|
||||
<p>设计和构建一个“最近最少使用”缓存,该缓存会删除最近最少使用的项目。缓存应该从键映射到值(允许你插入和检索特定键对应的值),并在初始化时指定最大容量。当缓存被填满时,它应该删除最近最少使用的项目。</p>
|
||||
|
||||
<p>它应该支持以下操作: 获取数据 <code>get</code> 和 写入数据 <code>put</code> 。</p>
|
||||
|
||||
<p>获取数据 <code>get(key)</code> - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。<br>
|
||||
写入数据 <code>put(key, value)</code> - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。</p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );
|
||||
|
||||
cache.put(1, 1);
|
||||
cache.put(2, 2);
|
||||
cache.get(1); // 返回 1
|
||||
cache.put(3, 3); // 该操作会使得密钥 2 作废
|
||||
cache.get(2); // 返回 -1 (未找到)
|
||||
cache.put(4, 4); // 该操作会使得密钥 1 作废
|
||||
cache.get(1); // 返回 -1 (未找到)
|
||||
cache.get(3); // 返回 3
|
||||
cache.get(4); // 返回 4
|
||||
</pre>
|
Reference in New Issue
Block a user