LRUCache满了,则先删除最后一个元素,再创建Node加入链表与创建哈希映射。 如果已经存在了,修改val值,并修改节点位置。 classLRUCache{ HashMap<Integer,Node> map;//创建哈希表DoubleList doubleList;//创建双向链表privateintcapacity;//代表LRU容量publicLRUCache(intcapacity){this.capacity = capacity; doubleList ...
146. LRU Cache ( Least Recently Used) importkotlin.collections.HashMap/*** 146. LRU Cache *https://leetcode.com/problems/lru-cache/description/* * 我们需要设计: * 1:使用HashMap:用于存储现有的key,value就是指向双向链表的节点 * 2:双向链表:在移除尾部元素、添加元素到头部、移动元素到头部都可以...
具体代码 一般的解法,通过维护一个数组,数组项存放了 key-value 键值对对象,每次需要遍历去寻找 key 值所在的数组下标操作。已经通过 leetCode 146 的检测。执行用时 : 720 ms。内存消耗 : 58.5 MB。function LRUCache(capacity) { this.capacity = capacity; // 最大限制 this.cache = []; }; ...
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. put(key, value)- Set or insert the ...
已经通过 leetCode 146 的检测。执行用时 : 720 ms。内存消耗 : 58.5 MB。 function LRUCache(capacity) { this.capacity = capacity; // 最大限制 this.cache = []; }; /** * @param {number} key * @return {number} */ LRUCache.prototype.get = function (key) { ...
已经通过 leetCode 146 的检测。执行用时 : 720 ms。内存消耗 : 58.5 MB。 function LRUCache(capacity) { this.capacity = capacity; // 最大限制 this.cache = []; }; /** * @param {number} key * @return {number} */ LRUCache.prototype.get = function (key) { ...