运用你所掌握的数据结构,设计和实现一个 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:VueLRU缓存策略

    1. /*
    2. * @lc app=leetcode.cn id=146 lang=javascript
    3. *
    4. * [146] LRU缓存机制
    5. */
    6. // @lc code=start
    7. /**
    8. * @param {number} capacity
    9. */
    10. var LRUCache = function(capacity) {
    11. this.capacity = capacity;
    12. this.result = {};
    13. this.queue = [];
    14. };
    15. /**
    16. * @param {number} key
    17. * @return {number}
    18. */
    19. LRUCache.prototype.get = function(key) {
    20. const index = this.queue.findIndex(item => item === key);
    21. if (index === -1) return -1;
    22. this.queue.splice(index, 1);
    23. this.queue.unshift(key);
    24. return this.result[key];
    25. };
    26. /**
    27. * @param {number} key
    28. * @param {number} value
    29. * @return {void}
    30. */
    31. LRUCache.prototype.put = function(key, value) {
    32. const index = this.queue.findIndex(item => item === key);
    33. if (index > -1) {
    34. this.queue.splice(index, 1);
    35. this.queue.unshift(key);
    36. this.result[key] = value;
    37. }
    38. else{
    39. if (this.capacity === this.queue.length) {
    40. const lastKey = this.queue.pop();
    41. this.result[lastKey] = undefined;
    42. }
    43. this.queue.unshift(key);
    44. this.result[key] = value;
    45. }
    46. };
    47. /**
    48. * Your LRUCache object will be instantiated and called as such:
    49. * var obj = new LRUCache(capacity)
    50. * var param_1 = obj.get(key)
    51. * obj.put(key,value)
    52. */
    53. // @lc code=end

    解法2:map

    1. /*
    2. * @lc app=leetcode.cn id=146 lang=javascript
    3. *
    4. * [146] LRU缓存机制
    5. */
    6. // @lc code=start
    7. /**
    8. * @param {number} capacity
    9. */
    10. var LRUCache = function (capacity) {
    11. this.map = new Map();
    12. this.capacity = capacity;
    13. };
    14. /**
    15. * @param {number} key
    16. * @return {number}
    17. */
    18. LRUCache.prototype.get = function (key) {
    19. if (this.map.has(key)) {
    20. const value = this.map.get(key);
    21. this.map.delete(key);
    22. this.map.set(key, value);
    23. return value;
    24. }
    25. return -1;
    26. };
    27. /**
    28. * @param {number} key
    29. * @param {number} value
    30. * @return {void}
    31. */
    32. LRUCache.prototype.put = function (key, value) {
    33. if (this.map.has(key)) {
    34. this.map.delete(key);
    35. this.map.set(key, value);
    36. }
    37. else {
    38. if (this.capacity === this.map.size) {
    39. const lastKey = [...this.map.keys()][0];
    40. this.map.delete(lastKey);
    41. }
    42. this.map.set(key, value);
    43. }
    44. };
    45. /**
    46. * Your LRUCache object will be instantiated and called as such:
    47. * var obj = new LRUCache(capacity)
    48. * var param_1 = obj.get(key)
    49. * obj.put(key,value)
    50. */
    51. // @lc code=end