“Offer 驾到,掘友接招!我正在参与2022春招打卡活动,点击查看活动详情。”
一、题目描述:
146. LRU 缓存
请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
值得一提Vue keep-alive组件就是通过这个算法完成的
LRU是Least Recently Used的缩写,即最近最少使用,是一种常用的页面置换算法,选择最近最久未使用的页面予以淘汰。该算法赋予每个页面一个访问字段,用来记录一个页面自上次被访问以来所经历的时间 t,当须淘汰一个页面时,选择现有页面中其 t 值最大的,即最近最少使用的页面予以淘汰。
实现 LRUCache 类:
LRUCache(int capacity)以 正整数 作为 容量 capacity 初始化 LRU 缓存int get(int key)如果关键字key存在于缓存中,则返回关键字的值,否则返回-1。void put(int key, int value)如果关键字key已经存在,则变更其数据值value;如果不存在,则向缓存中插入该组key-value。如果插入操作导致关键字数量超过capacity,则应该 逐出 最久未使用的关键字。
函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。
示例:
输入
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]
解释
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
提示:
1 <= capacity <= 3000
0 <= key <= 10000
0 <= value <= 105
最多调用 2 * 105 次 get 和 put
二、思路分析:
存储key-value 第一想到了Object,但是int key会使得Obj输出乱了次序, 再加数组保证顺序。先开干。但要求:函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。 数组找到数据肯定要遍历。
解法1 失败案例(实现功能但是大数据超时)
/**
* @param {number} capacity
*/
var LRUCache = function(capacity) {
this.box = new Object()
this.size = capacity
this.sortArr = new Array()
this.sort = (key)=>{
if(key in this.box){
this.sortArr.unshift(this.sortArr.splice(this.sortArr.indexOf(key),1)[0])
}
}
};
/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function(key) {
if(key in this.box){
this.sort(key)
return this.box[key]
}
return -1
};
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LRUCache.prototype.put = function(key, value) {
if(key in this.box){
this.sort(key)
} else {
this.sortArr.unshift(key)
}
if(this.sortArr.length>this.size){
let del_key = this.sortArr.pop()
delete this.box[del_key]
}
this.box[key] = value
};
/**
* Your LRUCache object will be instantiated and called as such:
* var obj = new LRUCache(capacity)
* var param_1 = obj.get(key)
* obj.put(key,value)
*/
解法2 Map
既然obj无序,数组得遍历。那就换一个数据类型,Map应运而生,有序、有长度、可迭代,无敌了,还要啥自行车。升级版Object
Map默认情况不包含任何键。只包含显式插入的键。Object有一个原型, 原型链上的键名有可能和你自己在对象上的设置的键名产生冲突。( 但可以new Object(null) )
Map的键可以是任意值,包括函数、对象或任意基本类型。Object的键必须是一个String或是Symbol。
Map中的 key 是有序的。因此,当迭代的时候,一个Map对象以插入的顺序返回键值。
Map的键值对个数可以轻易地通过size属性获取
Map是 iterable 的,所以可以直接被迭代。
/**
* @param {number} capacity
*/
var LRUCache = function(capacity) {
this.cache = new Map()
this.size = capacity
};
/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function(key) {
if(this.cache.has(key)){
let val = this.cache.get(key)
this.cache.delete(key)
this.cache.set(key, val)
return val
}
return -1
};
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LRUCache.prototype.put = function(key, value) {
if(this.cache.has(key)){
this.cache.delete(key)
} else if(this.cache.size >= this.size){ // 因为下面要添加,所以有=
this.cache.delete(this.cache.keys().next().value)
}
this.cache.set(key, value)
};
/**
* Your LRUCache object will be instantiated and called as such:
* var obj = new LRUCache(capacity)
* var param_1 = obj.get(key)
* obj.put(key,value)
*/
可能我天生愚笨,这搞了俩小时。。。晕了已经
解法3 手写链表结构
这个是看的力扣用时最少的解法,原理是
- 手写一个双向链表负责顺序,声明了头和尾两个守卫
- 用Map只记录有没有
/** * @param {number} capacity */
var LRUCache = function (capacity) {
head = new Node(0)
tail = new Node(0)
head.next = tail
tail.pre = head
this.count = 0
this.len = capacity
this.nodeMap = new Map()
};
/** * @param {number} key * @return {number} */
LRUCache.prototype.get = function (key) {
let node = this.nodeMap.get(key)
if (node) {
moveTohead(node)
return node.value
} else {
return -1
}
};
/** * @param {number} key * @param {number} value * @return {void} */
LRUCache.prototype.put = function (key, value) {
let node = this.nodeMap.get(key)
if (node) {
// 哈希表中已经有对应的key
node.value = value
moveTohead(node)
} else {
// 哈希表中不存在对应的key
// 新增
let newNode = new Node(key, value)
this.nodeMap.set(key, newNode)
this.count++
addNode(newNode)
// 删除最后一个
if (this.nodeMap.size > this.len) {
let delKey = tail.pre.key
this.nodeMap.delete(delKey)
removeLast()
this.count--
}
}
};
class Node {
constructor(key, value) {
this.key = key
this.value = value
this.pre = null
this.next = null
}
}
var deleteNode = function (node) {
node.pre.next = node.next
node.next.pre = node.pre
}
var addNode = function (node) {
node.next = this.head.next
this.head.next.pre = node
this.head.next = node
node.pre = this.head
}
var removeLast = function () {
this.tail.pre = this.tail.pre.pre
this.tail.pre.next = this.tail
}
var moveTohead = function (node) {
deleteNode(node)
addNode(node)
}