• 【每日一题Day336】LC146最近最少使用缓存 | 哈希表+链表


    最近最少使用缓存【LC146】

    请你设计并实现一个满足 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) 的平均时间复杂度运行。

    Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.

    Implement the LRUCache class:

    • LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
    • int get(int key) Return the value of the key if the key exists, otherwise return -1.
    • void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.

    The functions get and put must each run in O(1) average time complexity.

    • 思路

      • 使用HashMap存储key和value
      • 使用双链表记录最近使用的元素先后顺序:每次访问(get或者put)一个元素,都将其移到链表的末尾,那么当元素数量超过容量时,每次要移除的都是链表的第一个元素
    • 实现

      map集合的key为key,value为双链表的节点,节点存储key和value

      • get函数
        • map中存在key,将该节点移动到末尾
        • map中不存在key,直接返回-1
      • put函数
        • map中存在key,更新key对应的节点的value,并将其移动到末尾
        • map中不存在key,判断map的size是否大于等于容量
          • 是,移除头结点的下一个节点后将该节点放入map,并放入链表末尾
          • 否,直接将该节点放入map,并放入链表末尾
      • 因此需要编写辅助函数insertToTail,deleteNode,moveToTail
      class LRUCache {
          Map<Integer,ListNode> map;
          int capacity;
          ListNode head;
          ListNode tail;
          public LRUCache(int capacity) {
              this.map = new HashMap<>();
              this.capacity = capacity;
              head = new ListNode(-1, -1);
              tail = new ListNode(-1, -1);
              head.next = tail;
              tail.prev = head;
          }
          
          public int get(int key) {
              ListNode listNode = map.get(key);
              if (listNode == null){
                  return -1;
              }
              moveToTail(listNode,listNode.value);        
              return listNode.value;
          }
          
          public void put(int key, int value) {
              if (map.containsKey(key)){
                  moveToTail(map.get(key),value);
              }else{
                  if (map.size() == capacity){
                      map.remove(head.next.key);
                      deleteNode(head.next);               
                  }
                  ListNode newNode = new ListNode(key,value);
                  map.put(key,newNode);
                  insertTotail(newNode);
              }
          }
          public void deleteNode(ListNode listNode){
              listNode.prev.next = listNode.next;
              listNode.next.prev = listNode.prev;
          }
          public void moveToTail(ListNode listNode, int newValue){ 
              deleteNode(listNode);
              listNode.value = newValue;
              insertTotail(listNode);
          }
          public void insertTotail (ListNode listNode){
              tail.prev.next = listNode;
              listNode.prev = tail.prev;
              listNode.next = tail;
              tail.prev = listNode;
          }
      }
      class ListNode{
          public int key;
          public int value;
          public ListNode next;
          public ListNode prev;
          public ListNode(int k, int v){
              key = k;
              value = v;
          }
      }
      /**
       * Your LRUCache object will be instantiated and called as such:
       * LRUCache obj = new LRUCache(capacity);
       * int param_1 = obj.get(key);
       * obj.put(key,value);
       */
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68

  • 相关阅读:
    苹果电脑误删文件怎么找回?苹果电脑删了文件能恢复吗?苹果电脑文件删除怎么恢复
    【单片机】单片机入门指南
    vue-router(vue-router功能,跳转方式,路由守卫,路由懒加载,使用流程,3.x和4.x使用区别)
    mybatis中的多表查询
    第三章:人工智能深度学习教程-基础神经网络(第三节-Tensorflow 中的多层感知器学习)
    选择最适合的在线项目管理工具
    企业级镜像仓库Harbor的安装与配置
    【区块链实战】——实现简易版dapp【对于用户年龄的获取和更新】
    可重入函数
    软件定制vs现成,定制软件开发的优势
  • 原文地址:https://blog.csdn.net/Tikitian/article/details/133253950