77.LRU Cache(最近最久未使用算法)
Level:
Hard
题目描述:
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(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.
Follow up:
Could you do both operations in O(1) time complexity?
Example:
LRUCache cache = new LRUCache( 2 /* capacity */ );
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // returns 1
cache.put(3, 3); // evicts key 2
cache.get(2); // returns -1 (not found)
cache.put(4, 4); // evicts key 1
cache.get(1); // returns -1 (not found)
cache.get(3); // returns 3
cache.get(4); // returns 4
思路分析:
设置一个map存放对应的键和值,同时设置一个双向链表,来保存最近最久未使用的关系,如果访问一个键,键存在于map中,访问完成后,我们在链表中将该键删除,然后将其添加到链表的首部,表示最近刚访问过这个键,当缓存满了后,如果要添加一个键值对,我们要删除的就是位于链表尾部的键和其对应的值,因为它是最久未访问的值。
代码:
class LRUCache {
HashMap<Integer,Integer>cache=new HashMap<>();
LinkedList<Integer>keys=new LinkedList<>();
private static int sizeOfCache;
public LRUCache(int capacity) {
sizeOfCache=capacity;
}
public int get(int key) {
if(cache.get(key)!=null){
keys.remove(Integer.valueOf(key));//先在链表中删掉该键
keys.addFirst(key); //然后将该键放到链表首部,表示刚被访问
return cache.get(key);
}
return -1;
}
public void put(int key, int value) {
if(cache.get(key)!=null)
keys.remove(Integer.valueOf(key));
else if(keys.size()==sizeOfCache){ //存储块已满
int keyToRemove=keys.removeLast(); //链表最后一个键,代表最久未访问。
cache.remove(keyToRemove);
}
keys.addFirst(key);
cache.put(key,value);
}
}
/**
* 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);
*/
77.LRU Cache(最近最久未使用算法)的更多相关文章
- 页面置换算法——最近最久未使用算法(c语言实现)
操作系统实验:用C语言编程实现最近最久未使用置换算法(LRU) 最近最久未使用置换算法(LRU),全称Least Recently Used,是一种页面置换算法. 对于在内存中但又不用的数据块(内存块 ...
- 146 LRU Cache 最近最少使用页面置换算法
设计和实现一个 LRU(最近最少使用)缓存 数据结构,使它应该支持以下操作: get 和 put .get(key) - 如果密钥存在于缓存中,则获取密钥的值(总是正数),否则返回 -1.put(k ...
- 操作系统笔记(六)页面置换算法 FIFO法 LRU最近最久未使用法 CLOCK法 二次机会法
前篇在此: 操作系统笔记(五) 虚拟内存,覆盖和交换技术 操作系统 笔记(三)计算机体系结构,地址空间.连续内存分配(四)非连续内存分配:分段,分页 内容不多,就不做index了. 功能:当缺页中断发 ...
- LeetCode之LRU Cache 最近最少使用算法 缓存设计
设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...
- [转]如何用C++实现一个LRU Cache
[转自http://hawstein.com/posts/lru-cache-impl.html] LRU是Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法 ...
- 设计并实现一个LRU Cache
一.什么是Cache 1 概念 Cache,即高速缓存,是介于CPU和内存之间的高速小容量存储器.在金字塔式存储体系中它位于自顶向下的第二层,仅次于CPU寄存器.其容量远小于内存,但速度却可以接近CP ...
- LeetCode题解: LRU Cache 缓存设计
LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode 版权声明:本文为博主原创文章,遵循CC 4 ...
- LRU Cache数据结构简介
什么是LRU Cache LRU是Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法. 什么是Cache?狭义的Cache指的是位于CPU和主存间的快速RAM ...
- LRU算法实现 最近最久未使用
1.LRU算法实现 最近最久未使用(蚂蚁金服笔试题,本人亲自经历的[苦笑.jpg]) 实现原理:数组 主要功能:初始化.入队列 主要操作:数组元素移动 代码: package com.ch.evalu ...
随机推荐
- Informatica ODBC的使用
1.在服务器端配置odbc.ini 注意:添加环境变量才能生效 2.测试连通性 3.使用
- AtCoder Beginner Contest 137
前言 又是卡在了T4 , 总分 100 + 200 + 300 = 600pts rank 2449.以后还是要多积累比赛经验. A 输出a+b,a-b,a*b中最大的数,还要多简单?不挂代码了 B ...
- php session之多级目录存储
当选择以文件形式保存session到服务器时,需要制定保存路径.用到php.ini中的session.save_path,其有三种配置写法: session.save_path = "N;/ ...
- The list of list is modified unexpected, python
Be careful! The list of list is modified unexpected, python # code patch A: list = [1,2,3,4,5,6,7] p ...
- mybatisplus中使用SqlRunner出错
错误描述: SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@739488d7] was not registered ...
- Webstorm软件快捷键
默认配置-Eclipse的常用快捷键对照表 查找/代替 Webstorm快捷键 Eclipse快捷键 说明 ctrl+shift+N ctrl+shift+R 通过文件名快速查找工程内的文件(必记) ...
- mysql 1067 - Invalid default value for 'addtime'错误处理
错误描述 TABLE `bota_payment_closing` ( `id` int(11) NOT NULL AUTO_INCREMENT, `monthly` varchar(8) NOT N ...
- java单双引号转义问题
JavaScript代码:var str = '<a href="javascript:;" onclick="visaDetail(\'1\',' + value ...
- NOIp 基础数论知识点总结
推荐阅读 NOIp 数学知识点总结: https://www.cnblogs.com/greyqz/p/maths.html Basic 常用素数表:https://www.cnblogs.com/g ...
- python测试redis是否可以使用
前提打开redis服务,windows打开方式到redis的安装目录命令行输入redis-server from redis import StrictRedis redis = StrictRedi ...