146. LRU 缓存机制

运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制

实现 LRUCache 类:

  • LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存
  • int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1
  • void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。

进阶:你是否可以在 O(1) 时间复杂度内完成这两种操作?

示例:

  1. 输入
  2. ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
  3. [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
  4. 输出
  5. [null, null, null, 1, null, -1, null, -1, 3, 4]
  6. 解释
  7. LRUCache lRUCache = new LRUCache(2);
  8. lRUCache.put(1, 1); // 缓存是 {1=1}
  9. lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
  10. lRUCache.get(1); // 返回 1
  11. lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
  12. lRUCache.get(2); // 返回 -1 (未找到)
  13. lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
  14. lRUCache.get(1); // 返回 -1 (未找到)
  15. lRUCache.get(3); // 返回 3
  16. lRUCache.get(4); // 返回 4

提示:

  • 1 <= capacity <= 3000
  • 0 <= key <= 3000
  • 0 <= value <= 104
  • 最多调用 3 * 104getput

思路

用一个中间的数组来控制访问的次序。把最近访问,设置 的 key 放到数组的最后面。

代码

/**
 * @param {number} capacity
 */
var LRUCache = function (capacity) {
  this.map = {};
  this.capacity = capacity;
  this.arr = [];
};

/**
 * @param {number} key
 * @return {number}
 */
LRUCache.prototype.get = function (key) {
  let idx = this.arr.indexOf(key);
  if (idx > -1) {
    this.arr.splice(idx, 1);
    this.arr.push(key);
    return this.map[key];
  }
  return -1;
};

/**
 * @param {number} key
 * @param {number} value
 * @return {void}
 */
LRUCache.prototype.put = function (key, value) {
  let idx = this.arr.indexOf(key);
  if (idx > -1) {
    this.arr.splice(idx, 1);
  }
  if (this.arr.length == this.capacity) {
    this.arr.splice(0, 1);
    delete this.map[key];
  }
  this.arr.push(key);
  this.map[key] = value;
};
var LRUCache = function (capacity) {
  // 控制顺序
  this.setObj = new Set();
  // 实际存储
  this.mapObj = new Map();
  // 最大存储量
  this.max = capacity;
};

/**
 * @param {number} key
 * @return {number}
 */
LRUCache.prototype.get = function (key) {
  // console.log('get: ', this.setObj, this.mapObj)
  // 如果有这个key
  if (this.mapObj.has(key)) {
    // 获取这个key的值
    let val = this.mapObj.get(key);
    // 将顺序放到最近
    this.setObj.delete(key);
    this.setObj.add(key);
    return val;
  }
  return -1;
};

/**
 * @param {number} key
 * @param {number} value
 * @return {void}
 */
LRUCache.prototype.put = function (key, value) {
  // 如果put的值已经存在了, 直接替换
  if (this.setObj.has(key)) {
    this.setObj.delete(key);
    this.setObj.add(key);
    this.mapObj.set(key, value);
    return null;
  }
  // 如果不存在
  // 如果当前的数据已经到了最大存储量,就删除最远的元素
  if (this.setObj.size == this.max) {
    const index = this.setObj.values().next().value;
    this.setObj.delete(index);
    this.mapObj.delete(index);
  }
  // 设置值
  this.setObj.add(key);
  this.mapObj.set(key, value);
  // console.log('set: ', this.setObj, this.mapObj)
  return null;
};

复杂度分析

时间复杂度 day12.146. LRU 缓存机制 - 图1#card=math&code=O%28N%29)

空间复杂度 day12.146. LRU 缓存机制 - 图2#card=math&code=O%281%29)