Hard 146题 LRU Cache

本文介绍了一种使用双向链表和哈希表实现的LRU(最近最少使用)缓存机制。该机制通过get和set操作支持缓存数据的获取和设置,并在缓存达到容量限制时移除最近最少使用的项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Question:

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

Solution:

我的做法是map+list但是很慢~

public class LRUCache {
    private int capacity;
    private Map<Integer,Integer> map=new HashMap<Integer,Integer>();
    private List<Integer> arr=new ArrayList<Integer>();
    public LRUCache(int capacity) {
        this.capacity=capacity;

    }
    
    public int get(int key) {
        if(map.containsKey(key))
        {
            arr.remove((Integer)key);
            arr.add(key);
            return map.get(key);
        }
        return -1;
        
    }
    
    public void set(int key, int value) {
        
        if(map.containsKey(key))
        {
            arr.remove((Integer)key);
            arr.add(key);
            map.put(key, value);
        }
        else
        {
            if(map.size()>capacity-1)
            {
                map.remove(arr.get(0));
                arr.remove(0);
            }
            map.put(key,value);
            arr.add(key);
        }
        
    }
}
明天起来看double linked list!

构造函数那里,因为一个错搞了半天!!!

public class LRUCache {
    class DLinkedNode 
    {
	    int key;
	    int value;
	    DLinkedNode pre;
	    DLinkedNode post;
    }
    
    private void addAfterHead(DLinkedNode node)
    {
        node.pre=head;
        node.post=head.post;
        
        head.post.pre=node;
        head.post=node;
        
    }
    
    private void remove(DLinkedNode node)
    {
        DLinkedNode pre=node.pre;
        DLinkedNode post=node.post;
        
        pre.post=post;
        post.pre=pre;
    }
    
    private void moveToHead(DLinkedNode node)
    {
        this.remove(node);
        this.addAfterHead(node);
    }
    
    private DLinkedNode popTail()
    {
        DLinkedNode current=tail.pre;
        this.remove(current);
        return current;
    }
    private int capacity;
    private int count;
    private Map<Integer,DLinkedNode> map=new HashMap<Integer,DLinkedNode>();
    private DLinkedNode head;
    private DLinkedNode tail;
    
    public LRUCache(int capacity) {
        this.capacity=capacity;
        this.count=0;
        
        head=new DLinkedNode();
        head.pre=null;
        
        
        tail=new DLinkedNode();
       
        tail.post=null;
        
        
        head.post=tail;
        tail.pre=head;
        
    }
    
    public int get(int key) {
        
        DLinkedNode cur = map.get(key);
        if(cur==null)
            return -1;
        if(count!=1)
            this.moveToHead(cur);
        return cur.value;
        
    }
    
    public void set(int key, int value) {
        
        DLinkedNode cur=map.get(key);
        if(cur==null)
        {
            DLinkedNode newNode=new DLinkedNode();
            newNode.key=key;
            newNode.value=value;
            
            this.map.put(key,newNode);
            this.addAfterHead(newNode);
            ++count;
            
            if(count>capacity)
            {
                DLinkedNode t=this.popTail();
                this.map.remove(t.key);
                --count;
            }
            
        }
        else
        {
            cur.value=value;
            this.moveToHead(cur);
        }
        
    }
}



内容概要:本文档详细介绍了如何在MATLAB环境下实现CNN-GRU(卷积门控循环单元)混合模型的多输入单输出回归预测。项目旨在通过融合CNN的局部特征提取能力和GRU的时序依赖捕捉能力,解决传统序列模型在处理非线性、高维、多输入特征数据时的局限性。文档涵盖了项目背景、目标、挑战及其解决方案,强调了模型的轻量化、高效性和可视化全流程追踪等特点。此外,还提供了具体的应用领域,如智能电网负荷预测、金融时间序列建模等,并附有详细的代码示例,包括数据加载与预处理、网络结构定义、训练选项设置、模型训练与预测以及结果可视化等步骤。; 适合人群:对深度学习有一定了解,特别是对时间序列预测感兴趣的科研人员或工程师。; 使用场景及目标:①需要处理多输入单输出的非线性回归预测任务;②希望在MATLAB平台上快速实现并优化深度学习模型;③寻求一种高效、轻量且具有良好泛化能力的预测模型应用于实际场景中,如智能电网、金融分析、交通流量预测等领域。; 阅读建议:由于文档内容涉及较多的技术细节和代码实现,建议读者先熟悉CNN和GRU的基本概念,同时掌握MATLAB的基础操作。在阅读过程中,可以结合提供的代码示例进行实践操作,以便更好地理解和掌握CNN-GRU混合模型的构建与应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值