当前位置: 首页 > news >正文

LeetCode -- LRU Cache

题目描述:
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.


实现一个LRU的缓存,关于LRU缓存,可以概括为一个指定容量的的缓存,当超出容量时,拿掉最长时间没有使用的那个元素。

关于LRU的具体定义可以查看这个链接:

http://mcicpc.cs.atu.edu/archives/2012/mcpc2012/lru/lru.html



思路:
1. 使用哈希来存[key,value]
2. 维护一个集合,根据key的使用情况(最早使用的放最后)更新位置


实现代码:


public class LRUCache {


    private Dictionary<int, int> _cache; 
	private List<int> _usedKeys;
	
	private int _capacity;
	
    public LRUCache(int capacity)
	{
		_cache = new Dictionary<int, int>();
		_usedKeys = new List<int>();
		
        _capacity = capacity;
    }


    public int Get(int key)
	{
        if(!_cache.ContainsKey(key)){
			return -1;
		}
		else{
			_usedKeys.Remove(key);
			_usedKeys.Insert(0, key);
			
			return _cache[key];
		}
    }


    public void Set(int key, int value) 
	{
		if(_cache.ContainsKey(key)){
			_cache[key] = value;
			
			_usedKeys.Remove(key);
			_usedKeys.Insert(0, key);
		}
		else{
			if(_cache.Keys.Count < _capacity){
				_cache.Add(key, value);
				
				_usedKeys.Insert(0, key);
			}
			else
			{
				var removing = _usedKeys.Last();
				_usedKeys.RemoveAt(_usedKeys.Count - 1);
				
				_cache.Remove(removing);
				_cache.Add(key, value);
				
				_usedKeys.Insert(0, key);
			}
		}
    }
    
}


相关文章:

  • [Web 开发] 定制IE下载对话框的按钮(打开/保存)
  • LeetCode -- Min Stack
  • SQL2005CLR函数扩展-繁简转换
  • LeetCode -- Minimum Size Subarray Sum
  • LeetCode -- Number of 1 Bits
  • 对象属性拷贝(全匹配拷贝)
  • LeetCode -- Reorder List
  • 最近
  • LeetCode -- Search a 2D Matrix II
  • [IE编程] 打开/关闭IE8的光标浏览模式(Caret Browsing)
  • LeetCode -- 3Sum Closest
  • 使用反射将业务对象绑定到 ASP.NET 窗体控件
  • LeetCode -- 4Sum
  • LeetCode -- Binary Tree Level Order Traversal II
  • (ZT)一个美国文科博士的YardLife
  • Angular4 模板式表单用法以及验证
  • Docker 笔记(1):介绍、镜像、容器及其基本操作
  • ECMAScript入门(七)--Module语法
  • JavaScript设计模式系列一:工厂模式
  • JDK 6和JDK 7中的substring()方法
  • Linux Process Manage
  • mac修复ab及siege安装
  • PHP变量
  • React-生命周期杂记
  • tab.js分享及浏览器兼容性问题汇总
  • 多线程 start 和 run 方法到底有什么区别?
  • 全栈开发——Linux
  • 我从编程教室毕业
  • 自制字幕遮挡器
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组
  • #pragma once
  • #QT(一种朴素的计算器实现方法)
  • #stm32整理(一)flash读写
  • #我与Java虚拟机的故事#连载07:我放弃了对JVM的进一步学习
  • (2)(2.10) LTM telemetry
  • (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作...
  • (Matalb分类预测)GA-BP遗传算法优化BP神经网络的多维分类预测
  • (附源码)springboot 基于HTML5的个人网页的网站设计与实现 毕业设计 031623
  • (附源码)计算机毕业设计SSM教师教学质量评价系统
  • (三)docker:Dockerfile构建容器运行jar包
  • (三分钟)速览传统边缘检测算子
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (十七)Flask之大型项目目录结构示例【二扣蓝图】
  • (算法)前K大的和
  • (转)GCC在C语言中内嵌汇编 asm __volatile__
  • (转)Java socket中关闭IO流后,发生什么事?(以关闭输出流为例) .
  • **python多态
  • .NET 8.0 发布到 IIS
  • .NET Core日志内容详解,详解不同日志级别的区别和有关日志记录的实用工具和第三方库详解与示例
  • .NET Micro Framework 4.2 beta 源码探析
  • .NET Standard 支持的 .NET Framework 和 .NET Core
  • .NET6使用MiniExcel根据数据源横向导出头部标题及数据
  • .net访问oracle数据库性能问题
  • .net连接oracle数据库
  • .net下的富文本编辑器FCKeditor的配置方法