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(最近最久未使用算法)的更多相关文章

  1. 页面置换算法——最近最久未使用算法(c语言实现)

    操作系统实验:用C语言编程实现最近最久未使用置换算法(LRU) 最近最久未使用置换算法(LRU),全称Least Recently Used,是一种页面置换算法. 对于在内存中但又不用的数据块(内存块 ...

  2. 146 LRU Cache 最近最少使用页面置换算法

    设计和实现一个  LRU(最近最少使用)缓存 数据结构,使它应该支持以下操作: get 和 put .get(key) - 如果密钥存在于缓存中,则获取密钥的值(总是正数),否则返回 -1.put(k ...

  3. 操作系统笔记(六)页面置换算法 FIFO法 LRU最近最久未使用法 CLOCK法 二次机会法

    前篇在此: 操作系统笔记(五) 虚拟内存,覆盖和交换技术 操作系统 笔记(三)计算机体系结构,地址空间.连续内存分配(四)非连续内存分配:分段,分页 内容不多,就不做index了. 功能:当缺页中断发 ...

  4. LeetCode之LRU Cache 最近最少使用算法 缓存设计

    设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...

  5. [转]如何用C++实现一个LRU Cache

    [转自http://hawstein.com/posts/lru-cache-impl.html] LRU是Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法 ...

  6. 设计并实现一个LRU Cache

    一.什么是Cache 1 概念 Cache,即高速缓存,是介于CPU和内存之间的高速小容量存储器.在金字塔式存储体系中它位于自顶向下的第二层,仅次于CPU寄存器.其容量远小于内存,但速度却可以接近CP ...

  7. LeetCode题解: LRU Cache 缓存设计

    LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode   版权声明:本文为博主原创文章,遵循CC 4 ...

  8. LRU Cache数据结构简介

    什么是LRU Cache LRU是Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法. 什么是Cache?狭义的Cache指的是位于CPU和主存间的快速RAM ...

  9. LRU算法实现 最近最久未使用

    1.LRU算法实现 最近最久未使用(蚂蚁金服笔试题,本人亲自经历的[苦笑.jpg]) 实现原理:数组 主要功能:初始化.入队列 主要操作:数组元素移动 代码: package com.ch.evalu ...

随机推荐

  1. Taro -- 定义全局变量

    Taro定义全局变量 方法1:在taro中 getApp()只能取到一开始定义的值,并不能取到改变后的值 // app.js文件中 class App extends Component { cons ...

  2. Linux性能优化从入门到实战:03 CPU篇:CPU上下文切换

      linux操作系统是将CPU轮流分配给任务,分时执行的.而每次执行任务时,CPU需要知道CPU寄存器(CPU内置的内存)和程序计数器PC(CPU正在执行指令和下一条指令的位置)值,这些值是CPU执 ...

  3. BZOJ2440/洛谷P4318 [中山市选2011]完全平方数 莫比乌斯函数

    题意:找到第k个无平方因子数. 解法:这道题非常巧妙的运用了莫比乌斯函数的性质! 解法参考https://www.cnblogs.com/enzymii/p/8421314.html这位大佬的.这里我 ...

  4. k8s 1.9.0-手动安装-2

    1 下载etcd新版 https://github.com/coreos/etcd/releases 直接下载k8s的二进制包 https://github.com/kubernetes/kubern ...

  5. python其他篇(1)

    1.跳过anaconda直接打开spyder或qtconsole: 打开终端,输入spyder或者jupyter-qtconsole 2.conda安装jpype1和pyhanlp: 参考:http: ...

  6. POJ - 3481 splay板子

    Double Queue 默写splay板子 很多细节问题... #include<cstdio> #include<iostream> using namespace std ...

  7. .Net-WCF-图书:《WCF编程》

    ylbtech-.Net-WCF-图书:<WCF编程> <WCF编程>是2008年1月机械工业出版社出版的图书,作者是Juval Lowy.Clemens Vasters. 1 ...

  8. JS-让浏览器兼容ES6特性

    babel:将 ES6 翻译为 ES5 PS:ie 还不支持 import 和 export 还是用 gulp 打包一下吧

  9. 126、TensorFlow Session的执行

    # tf.Session.run 方法是一个执行tf.Operation或者计算tf.Tensor的一个主要的机制 # 你可以传递一个或者多个tf.Operation或者tf.Tensor对象来给tf ...

  10. Java常用工具——java字符串

    一.String常用字符串 package com.imooc.string; public class StringDemo { public static void main(String[] a ...