详解leetcode146题【LRU (最近最少使用) 缓存机制】(附js最优解法!)

5,861 阅读4分钟

leetcode 146. LRU (最近最少使用) 缓存机制

题目描述

运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。

获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。

进阶:

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

示例:

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

解题思路:

搞清楚两个问题就行,执行put操作的时候关注

一、缓存数据的变化

分为两种情况:

1.缓存不满

命中缓存(缓存中存在该值),则缓存无任何变化

未命中缓存(缓存中不存在该值),缓存中加入该值

2.缓存已满

命中缓存,缓存无变化

未命中缓存,删掉缓存末尾的值,之后缓存中加入该值

从以上分析,要想找到缓存末尾的值,我想到两个办法。

(1)将缓存有序化(有序化涉及到排序,增加算法复杂度,所以我不用这个方法)

(2)设置一个指针从内存第一个数开始跟踪缓存末尾的值

二、内存中增加数据时内存的变化

也是两种情况 1.内存中不存在该值(新数据)

直接将该值置于内存首部

2.内存中已经存在该值(旧数据)

更新内存中值的顺序,规则是将改值的前一个节点的下一个节点设置为该值的下一个节点,然后该值置于内存首部(基本链表操作)

这里需要考虑部分特殊情况,比如内存为空的情况下连续执行以下操作

put(1, 1);
put(1, 1);

所以更新的规则要兼容以上情况

执行get的时候,如果缓存中存在get的数据,则更新缓存顺序,跟以上一样。

代码:

let LRUCache = function(capacity) {
    this.cacheSize = capacity;
    // 缓存计数器
    this.cacheIndex = 0;
    this.cacheSet = new Set();
    // 内存头节点
    this.head = null;
    // 缓存尾节点
    this.cacheShift = null;
    this.memory = {};
};

LRUCache.prototype.get = function(key) {
    let val;
    const { cacheSet, memory } = this;
    if (cacheSet.has(key)) {
        val = memory[key].value;
        console.log(memory[key].value)
        // get 最后一个节点
        if (memory[key].next == null) {
            return val;
        }
        if (memory.cacheShift === memory[key] && memory.cacheShift.next) {
            memory.cacheShift = memory.cacheShift.next;
        }
        this.memorySort(key);
    } else {
        val = -1;
        console.log(-1);
    }
    
    return val;
};

LRUCache.prototype.put = function(key, value) {
    const { cacheSet, memory } = this;

    if (this.cacheIndex < this.cacheSize) {
        !cacheSet.has(key) && this.cacheIndex++;
        cacheSet.add(key)
    } else {
        if (!cacheSet.has(key)) {
            cacheSet.delete(memory.cacheShift.key);
            memory.cacheShift.next && (memory.cacheShift = memory.cacheShift.next);
            cacheSet.add(key);
        }
    }

    // 内存中有值
    if (memory.head) {
        // 内存中不存在该节点
        if (!memory[key]) {
            memory[key] = {
                prev: memory.head,
                next: null
            }
            memory.head.next = memory[key];
            memory.head = memory[key];
        } else { // 内存中存在节点
            if (memory.cacheShift === memory[key] && memory.cacheShift.next) {
                memory.cacheShift = memory[key].next;
            }
            this.memorySort(key);
        }
    } else {  // 内存为空,该节点为第一个节点
        memory[key] = {
            prev: null,
            next: null
        };
        memory.cacheShift = memory.head = memory[key];
    }

    memory[key].key = key;
    memory[key].value = value;
};

LRUCache.prototype.memorySort = function(key) {
    const { memory } = this;
    // get 的不是最后一个节点
    if (memory[key].next != null) {
        if (memory[key].prev != null) {
            memory[key].prev.next = memory[key].next;
        } else {    // 第一个节点
            memory[key].next.prev = null;
        }
        memory[key].next.prev = memory[key].prev;
        memory[key].prev = memory.head;
        memory[key].next = null;
        memory.head.next = memory[key];
        memory.head = memory[key];
    } 
}

以上,是我第一感觉的做法。为什么说是第一感觉,首先,题目要求O(1)的复杂度,所以我不能用js中较为方便操作的数组。期次,不能用对象,因为用对象无法知道缓存的顺序。所以我只能想到链表的操作,一旦用到链表,免不了各种增删改查的操作,所以代码上会复杂不少。

然而,我的一位同事的做法,让我震惊了。如下:

class LRUCache {
  constructor(capacity) {
    this.capacity = capacity
    this.map = new Map()
  }

  get(key) {
    let val = this.map.get(key)
    if (typeof val === 'undefined') { return -1 }
    this.map.delete(key)
    this.map.set(key, val)
    return val
  }

  put(key, value) {
    if (this.map.has(key)) { 
      this.map.delete(key) 
    }

    this.map.set(key, value)
    let keys = this.map.keys()
    while (this.map.size > this.capacity) { this.map.delete(keys.next().value) }
  }
}

这里他用的是map,为什么map可以。这里分析下。

map.keys().next()可以取得到他排位第一的键值,map.put()操作类似数组的push操作,将值保存在最顶的位置,这两点就是最关键的,也正是我没想到的。这样一来,就能清除的排序而且对map的操作复杂度为O(1),比操作对象还快。不仅在代码上及其优美,算法复杂度也是最优的。

感触:js为我们封装了不少函数,熟练掌握,你就能如虎添翼。