cache.put(1, 1); // 缓存是 {1=1} cache.put(2, 2); // 缓存是 {1=1, 2=2} cache.get(1); // 返回 1,缓存更新为 {2=2, 1=1} cache.put(3, 3); // 淘汰最久未使用的 key 2,缓存变成 {1=1, 3=3} cache.get(2); // 返回 -1(未找到) 三、误区:直接用数组/
实现LRUCache 类: LRUCache(int capacity) 以正整数 作为容量 capacity 初始化 LRU 缓存 int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1。 void put(int key, int value) 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果...
Python3 Javaclass LRUCache(collections.OrderedDict): def __init__(self, capacity: int): super().__init__() self.capacity = capacity def get(self, key: int) -> int: if key not in self: return -1 self.move_to_end(key) ...
LRUCache cache = new LRUCache( 2 /* capacity */ ); cache.put(1, 1); cache.put(2, 2); cache.get(1); // returns 1 cache.put(3, 3); // evicts key 2 cache.get(2); // returns -1 (not found) cache.put(4, 4); // evicts key 1 cache.get(1); // returns -1 (not ...
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} ...
【LeetCode】LRU Cache 解决报告 插话:只写了几个连续的博客,博客排名不再是实际“远在千里之外”该。我们已经进入2一万内。 再接再厉。油! Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset....
Can you solve this real interview question? LRU Cache - Design a data structure that follows the constraints of a Least Recently Used (LRU) cache [https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU]. Implement the LRUCache class: * LRUCache(
Java源代码: ```java public class LRUCache { class DLinkedNode { int key; int value; DLinkedNode prev; DLinkedNode next; public DLinkedNode() {} public DLinkedNode(int _key, int _value) {key = _key; value = _value;} }
【LeetCode】LRU Cache 解决报告 插话:只写了几个连续的博客,博客排名不再是实际“远在千里之外”该。我们已经进入2一万内。 再接再厉。油! Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset....
1.1 LRUCache(int capacity)以正整数作为容量capacity初始化LRU缓存个数 1.2 int get(int key) 如果key关键字存在于缓存中,则返回关键字的值,否则返回-1 1.3 void put(int key, int value)如果关键字已经存在,则变更其数据值,如果关键字不存在,则插入改组关键字。当缓存容量达到上限时,他应该在写入新数据之前...