• LeetCode 146. LRU 缓存


    原题链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

    感谢博主题解点拨思路:LeetCode 0146. LRU 缓存:双向链表 + 哈希-CSDN博客

    题目描述

    请你设计并实现一个满足  LRU (最近最少使用) 缓存 约束的数据结构。

    实现 LRUCache 类:

    • LRUCache(int capacity)正整数 作为容量 capacity 初始化 LRU 缓存
    • int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1
    • void put(int key, int value) 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。

    函数 getput 必须以 O(1) 的平均时间复杂度运行。

    样例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

    Tag

    双向链表 哈希表

    题目分析

    LRU算法需要维护最久未使用的关键字,题目要求put和get都是O(1)复杂度,所使用的数据结构需要O(1)查询到关键字所在位置,并将位置调整到最近使用,同时给出关键字需要O(1)时间内得到value值。

    思路

    get中查询value做到O(1)比较容易,可以使用哈希表实现(此题使用map)。

    put中在O(1)时间内使新加入元素调整到最近使用很多数据结构都能做到,诸如链表插头(尾)、队列入队、入栈等,调整节点位置包括找到节点位置+移动节点,O(1)调整位置直接用这些数据结构则不行。因此我们采用map的方式存储,根据关键字直接查到链表节点的地址,从而根据地址可以直接对节点进行操作。

    设计链表头结点为最近使用,尾节点为最久未使用。节点位置在O(1)时间内找到后,需要将节点从原位置调整至队首,同时超过缓存容量还要删除节点(队尾元素)。使用单链表可以做到移动节点,但无法快速删除尾节点,而使用双向链表删除尾节点后,可以根据指向前一个节点的prev指针将前一个节点设为尾节点,下次溢出时可以快速删除。

    C++代码

    1. class node{
    2. public:
    3. node *prev,*next;
    4. int key;
    5. int value;
    6. node(node *prev,node *next,int key,int value)
    7. {
    8. this->key = key;
    9. this->value = value;
    10. this->prev = prev;
    11. this->next = next;
    12. }
    13. };
    14. class LRUCache {
    15. private:
    16. int capacity;
    17. unordered_map<int,node*> mp;//node*:节点指针,存储链表中某个节点地址
    18. node *head,*tail;
    19. public:
    20. LRUCache(int capacity) {
    21. this->capacity = capacity;
    22. head = new node(nullptr,nullptr,0,0);
    23. tail = new node(head,nullptr,0,0);
    24. head->next = tail;
    25. }
    26. void adjust(node* temp) //将node移动到队首
    27. {
    28. temp->next = head->next;
    29. temp->prev = head;
    30. head->next = temp;
    31. temp->next->prev = temp;
    32. }
    33. int get(int key) {
    34. if(mp.count(key)){
    35. node* move = mp[key];
    36. /*删除节点*/
    37. move->prev->next = move->next;
    38. move->next->prev = move->prev;
    39. adjust(move);
    40. return mp[key]->value;
    41. }
    42. return -1;
    43. }
    44. void put(int key, int value) {
    45. if(mp.count(key)){
    46. node* temp = mp[key];
    47. mp[key]->value = value;//改值
    48. //删节点
    49. temp->prev->next = temp->next;
    50. temp->next->prev = temp->prev;
    51. adjust(temp);//调位置
    52. }else{
    53. node* newnode = new node(head,head->next,key,value);
    54. head->next->prev = newnode;
    55. head->next = newnode;
    56. mp[key] = newnode;
    57. }
    58. if(mp.size()>capacity){
    59. node* del = tail->prev;
    60. tail->prev = del->prev;
    61. del->prev->next = tail;
    62. mp.erase(del->key);
    63. }
    64. }
    65. };
    66. /**
    67. * Your LRUCache object will be instantiated and called as such:
    68. * LRUCache* obj = new LRUCache(capacity);
    69. * int param_1 = obj->get(key);
    70. * obj->put(key,value);
    71. */

    Debug

    以下是我自己本地debug时用的代码

    1. int main()
    2. {
    3. freopen("stdin.txt","r",stdin);
    4. string op;
    5. LRUCache* obj = nullptr;
    6. while(cin>>op){
    7. if(op=="cache"){
    8. int capacity;
    9. cin>>capacity;
    10. obj = new LRUCache(capacity);
    11. cout<<"null ";
    12. }else if(op == "get"){
    13. int key;
    14. cin>>key;
    15. cout<get(key)<<" ";
    16. }else{
    17. int key,value;
    18. cin>>key>>value;
    19. obj->put(key,value);
    20. cout<<"null ";
    21. }
    22. }
    23. }
    1. cache
    2. 2
    3. put
    4. 1 1
    5. put
    6. 2 2
    7. get
    8. 1
    9. put
    10. 3 3
    11. get
    12. 2
    13. put
    14. 4 4
    15. get
    16. 1
    17. get
    18. 3
    19. get
    20. 4

  • 相关阅读:
    【算法与数据结构】236、LeetCode二叉树的最近公共祖先
    外贸开发信主题怎么写?营销邮件标题推荐?
    SIGIR2024| RAREMed: 不放弃任何一个患者——提高对罕见病患者的药物推荐准确性
    “绿色低碳+数字孪生“双轮驱动,解码油气管道站升级难点 | 图扑软件
    通过文章id递归查询所有评论(xml)
    ORA-22992 cannot use LOB locators selected from remote tables
    深拷贝与浅拷贝
    Spring中自定义依赖注入对象注入Controller中,优雅的解决用户鉴权问题(HandlerInterceptorAdapter)
    linux服务器配置深度学习环境一些命令
    基于Matlab求解高教社杯全国大学生数学建模竞赛(CUMCM2004A题)-奥运会临时超市网点设计(附上源码+数据)
  • 原文地址:https://blog.csdn.net/liangcha_xyy/article/details/133271153