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

57 篇文章 0 订阅
10 篇文章 0 订阅

最近最少使用缓存【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);
     */
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值