• 算法题练习——NC93 设计LRU缓存结构


    描述

            设计LRU(最近最少使用)缓存结构,该结构在构造时确定大小,假设大小为 capacity ,操作次数是 n ,并有如下功能:
    1. Solution(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存
    2. get(key):如果关键字 key 存在于缓存中,则返回key对应的value值,否则返回 -1 。
    3. set(key, value):将记录(key, value)插入该结构,如果关键字 key 已经存在,则变更其数据值 value,如果不存在,则向缓存中插入该组 key-value ,如果key-value的数量超过capacity,弹出最久未使用的key-value

    提示:

    1.某个key的set或get操作一旦发生,则认为这个key的记录成了最常使用的,然后都会刷新缓存。
    2.当缓存的大小超过capacity时,移除最不经常使用的记录。
    3.返回的value都以字符串形式表达,如果是set,则会输出"null"来表示(不需要用户返回,系统会自动输出),方便观察
    4.函数set和get必须以O(1)的方式运行
    5.为了方便区分缓存里key与value,下面说明的缓存里key用""号包裹

    数据范围:1≤capacity<=10^5,0≤key,val≤2×10^9 ,1≤n≤10^5

    示例1

    输入:

    ["set","set","get","set","get","set","get","get","get"],[[1,1],[2,2],[1],[3,3],[2],[4,4],[1],[3],[4]],2

    返回值:

    ["null","null","1","null","-1","null","-1","3","4"]

    说明:

    我们将缓存看成一个队列,最后一个参数为2代表capacity,所以
    Solution s = new Solution(2);
    s.set(1,1); //将(1,1)插入缓存,缓存是{"1"=1},set操作返回"null"
    s.set(2,2); //将(2,2)插入缓存,缓存是{"2"=2,"1"=1},set操作返回"null"
    output=s.get(1);// 因为get(1)操作,缓存更新,缓存是{"1"=1,"2"=2},get操作返回"1"
    s.set(3,3); //将(3,3)插入缓存,缓存容量是2,故去掉某尾的key-value,缓存是{"3"=3,"1"=1},set操作返回"null" 
    output=s.get(2);// 因为get(2)操作,不存在对应的key,故get操作返回"-1"
    s.set(4,4); //将(4,4)插入缓存,缓存容量是2,故去掉某尾的key-value,缓存是{"4"=4,"3"=3},set操作返回"null" 
    output=s.get(1);// 因为get(1)操作,不存在对应的key,故get操作返回"-1"
    output=s.get(3);//因为get(3)操作,缓存更新,缓存是{"3"=3,"4"=4},get操作返回"3"
    output=s.get(4);//因为get(4)操作,缓存更新,缓存是{"4"=4,"3"=3},get操作返回"4"  

    JavaScript Node题解:

    1. /**
    2. * @param {number} capacity
    3. */
    4. var Solution = function(capacity) {
    5. // write code here
    6. this.max = capacity;
    7. this.map = new Map();
    8. };
    9. /**
    10. * @param {number} key
    11. * @return {number}
    12. */
    13. Solution.prototype.get = function(key) {
    14. // write code here
    15. if(this.map.has(key)){
    16. let value = this.map.get(key);
    17. this.map.delete(key);
    18. this.map.set(key,value);
    19. return value;
    20. }else{
    21. return -1
    22. }
    23. };
    24. /**
    25. * @param {number} key
    26. * @param {number} value
    27. * @return {void}
    28. */
    29. Solution.prototype.set = function(key, value) {
    30. // write code here
    31. if(this.map.size === this.max){
    32. let last = this.map.keys().next();
    33. this.map.delete(last.value);
    34. }
    35. this.map.set(key,value);
    36. };
    37. module.exports = {
    38. Solution : Solution
    39. };
    40. /**
    41. * Your Solution object will be instantiated and called as such:
    42. * var solution = new Solution(capacity)
    43. * var output = solution.get(key)
    44. * solution.set(key,value)
    45. */

    python代码解: 

    1. class Solution:
    2. def __init__(self, capacity: int):
    3. # write code here
    4. self.capacity = capacity
    5. self.nums = {}
    6. self.lru = []
    7. def get(self, key: int) -> int:
    8. # write code here
    9. val = '-1'
    10. if key in self.nums:
    11. val = self.nums[key]
    12. self.lru.remove(key)//移除lru缓存中的key
    13. self.lru.append(key)//重新添加key进入缓存,实现更新
    14. return val
    15. def set(self, key: int, value: int) -> None:
    16. # write code here
    17. if len(self.lru) == self.capacity:
    18. //超过结构长度即移除最不常访问的key
    19. self.nums.pop(self.lru[0])
    20. del self.lru[0]
    21. self.nums[key] = value
    22. self.lru.append(key)
    23. # Your Solution object will be instantiated and called as such:
    24. # solution = Solution(capacity)
    25. # output = solution.get(key)
    26. # solution.set(key,value)
  • 相关阅读:
    likou617合并二叉树
    前端需要知道的JSON.stringify的正确用法
    Vue学习第16天——全局事件总线$bus的理解
    Qt 画自定义饼图统计的例子
    spring整合mybatis
    不存在类型变量 A, T 的实例,使 Collector<T, A, List<T>> 符合 Supplier<R>
    小白学习MySQL - 增量统计SQL的需求
    安卓案例:利用URLConnection下载图片
    深入理解虚拟/物理地址转换,页表--基于ARMV8
    vscode调试webpack项目的方法
  • 原文地址:https://blog.csdn.net/weixin_53919192/article/details/126708081