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);
 */
相关推荐
HugoStudio_SWAN32 分钟前
洛谷 P1420 / P1179 / B4262 最长连号、数字统计与词频统计——统计的三种面孔
c++·学习·程序人生·算法
青 春 记 忆1 小时前
LeetCode 350. 两个数组的交集 II|Python 解法详解
python·算法·leetcode
阳明山水2 小时前
销量预测2025—2026:从模型竞赛到系统融合的范式转型
人工智能·深度学习·算法·机器学习·架构
桐盛科技3 小时前
拒绝“漂绿”风险:基于边缘计算的碳排放数据清洗算法与实时校验逻辑
人工智能·算法·边缘计算
Tongzhi20263 小时前
通芝科技考勤软件三大发展趋势:AI大模型、无感识别与数据底座
大数据·科技·算法·爬山算法
eBest数字化转型方案3 小时前
Route Optimization for FMCG:多目标排线算法的工程实现
大数据·人工智能·算法
residual_fan4 小时前
持续对比强化学习(Continual Contrastive Reinforcement Learning)论文分享
人工智能·算法·数据挖掘·数据分析
s_w.h4 小时前
【 计网 】序列化与反序列化
linux·服务器·网络·算法·bash
信奥卷王4 小时前
2025年09月GESPC++五级真题解析(含视频)
算法
白狐_7984 小时前
408 数据结构|外部排序:流程与 k 路归并
数据结构·算法