leetcode刷题日志-146LRU缓存

思路:使用hashmap储存key,vaule,使用双向链表以快速查到尾结点(待逐出的节点),链表的题一定要在纸上画一下,不然连着连着就不知道连在哪里去了

java 复制代码
class LRUCache {
    public class ListNode {
      int key;
      int value;
      ListNode next;
      ListNode pre;
      ListNode() {}
      ListNode(int key , int value) { this.key = key; this.value = value;}
      ListNode(int kye, int value, ListNode next) { this.key = key;this.value = value;this.next = next;}
      ListNode(int kye, int value, ListNode next,ListNode pre) { this.key = key;this.value = value;this.next = next;this.pre = pre;}
  }
    ListNode head;//头节点
    ListNode tail;//指向尾结点的前一个节点,方便逐出最久未使用关键字
    int capacity; //储存容量
    int cur_capacity;//储存当前容量
    Map<Integer,ListNode> map;//储存节点
    
    public LRUCache(int capacity) {
        this.head = new ListNode(0,0,null,null);
        this.capacity = capacity;
        this.cur_capacity = 0;
        this.map = new HashMap<>();
        this.tail = new ListNode(0,0,null,head);
        this.head.next = tail;
    }
    
    public int get(int key) {
        if(map.containsKey(key))  //最近使用到的key,将位置提前
        {
            if(map.get(key).next != null)
            {
                map.get(key).next.pre = map.get(key).pre;
                map.get(key).pre.next = map.get(key).next;
            }
            map.get(key).next = head.next;
            map.get(key).next.pre = map.get(key);
            map.get(key).pre = head;
            head.next = map.get(key);
            return map.get(key).value;
        }
        else
            return -1;
    }
    
    public void put(int key, int value) {
        if(map.containsKey(key)) //存在,将位置提前
        {
            map.get(key).value = value;
            if(map.get(key).next != null)
            {
                map.get(key).next.pre = map.get(key).pre;
                map.get(key).pre.next = map.get(key).next;
            }
            map.get(key).next = head.next;
            map.get(key).next.pre = map.get(key);
            map.get(key).pre = head;
            head.next = map.get(key);
        }
        else{//不存在
            if(this.cur_capacity < this.capacity) //容量足够,直接添加到头
            {
                ListNode temp = new ListNode();
                temp.key = key;
                temp.value = value;
                temp.next = head.next;
                if(temp.next != null)
                temp.next.pre = temp;
                temp.pre = head;
                head.next = temp;
                cur_capacity++;
                map.put(key,temp);
            }
            else//容量不够,移出尾结点,添加新节点到头
            {

                ListNode tail_pre = tail.pre;
                map.remove(tail_pre.key);
                tail.pre.pre.next = tail;
                tail.pre.next = null;
                tail.pre = tail.pre.pre;
                tail_pre.pre = null;
                ListNode temp = new ListNode();
                temp.key = key;
                temp.value = value;
                temp.next = head.next;
                temp.next.pre = temp;
                temp.pre = head;
                head.next = temp;
                map.put(key,temp);
            }
        }
    }
}

/**
 * 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);
 */
相关推荐
数研小生6 小时前
构建命令行单词记忆工具:JSON 词库与艾宾浩斯复习算法的完美结合
算法·json
芒克芒克6 小时前
LeetCode 题解:除自身以外数组的乘积
算法·leetcode
Python 老手6 小时前
Python while 循环 极简核心讲解
java·python·算法
@Aurora.6 小时前
优选算法【专题九:哈希表】
算法·哈希算法·散列表
爱看科技6 小时前
微美全息(NASDAQ:WIMI)研究拜占庭容错联邦学习算法,数据安全与隐私保护的双重保障
算法
qq_417129257 小时前
C++中的桥接模式变体
开发语言·c++·算法
YuTaoShao7 小时前
【LeetCode 每日一题】3010. 将数组分成最小总代价的子数组 I——(解法二)排序
算法·leetcode·排序算法
he___H7 小时前
Redis高级数据类型
数据库·redis·缓存
吴维炜9 小时前
「Python算法」计费引擎系统SKILL.md
python·算法·agent·skill.md·vb coding
惊讶的猫9 小时前
Redis双写一致性
数据库·redis·缓存