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 cache,这里自定义越靠近链表头的node,在越近的时间使用过。支持两个操作——get和set:

  1. get(key):如果链表中有key对应的node,返回该node的值,并把node移到链表头部(最近使用过);如果没有,返回-1;
  2. set(key,value):如果链表中已经有key对应的node,修改对应node的值为value(但不需要把这个node放在头结点处);如果链表中没有key对应的node,那么要新建node插入链表中,但此时要看链表是否还有空间。如果没有,就将尾部node(最少使用的node)删除,然后在头部插入新的node。

为了提高查找效率,这里使用一个hashMap存放<key,node>键值对,这样就可以在O(1)的时间判断cache中是否存在对应的key。

代码如下:

 public class LRUCache {
private class Node{
int value;
int key;
Node before;
Node after;
public Node(int key,int value){
this.value = value;
this.key = key;
before = null;
after = null;
}
} private int capacity;
private HashMap<Integer, Node> map = new HashMap<Integer,Node>();
private Node headNode = new Node(-1, -1);
private Node tailNode = new Node(-1, -1); public LRUCache(int capacity) {
this.capacity = capacity;
headNode.after = tailNode;
tailNode.before = headNode;
} public void move_to_head(Node current){
current.after = headNode.after;
headNode.after = current;
current.before = headNode;
current.after.before = current;
}
public int get(int key) {
//if we don't have this node
if(!map.containsKey(key))
return -1; //if we have this node, get it and move it to head
Node current = map.get(key);
current.before.after = current.after;
current.after.before = current.before;
move_to_head(current); return map.get(key).value;
} public void set(int key, int value) {
//if we already have this node,just change its value
if(get(key) != -1){
map.get(key).value = value;
return;
} //if we indeed don't have this node, we first check capacity
if(map.size() == capacity){
map.remove(tailNode.before.key);
tailNode.before.before.after = tailNode;
tailNode.before = tailNode.before.before;
} //now we are sure we have space for this new node,put it ahead of the list
Node newNode = new Node(key, value);
map.put(key, newNode);
move_to_head(newNode);
}
}

在实现的时候,还设置了两个node:head和tail,真正的cache数据节点存放在二者之间。

【leetcode刷题笔记】LRU Cache的更多相关文章

  1. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  2. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  3. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  4. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  5. LeetCode刷题笔记(1-9)

    LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...

  6. leetcode刷题笔记

    (1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...

  7. leetcode刷题笔记08 字符串转整数 (atoi)

    题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...

  8. LeetCode刷题笔记-回溯法-分割回文串

    题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab"输出:[ ["aa", ...

  9. leetcode刷题笔记231 2的幂

    题目描述: 给定一个整数,写一个函数来判断它是否是2的幂. 题目分析: 判断一个整数是不是2的幂,可根据二进制来分析.2的幂如2,4,8,等有一个特点: 二进制数首位为1,其他位为0,如2为10,4为 ...

随机推荐

  1. 公告板shader

    Shader "Custom/LightPoint" { Properties { _MainTex ("Main Tex", 2D) = "whit ...

  2. liunx 下安装 php_screw 扩展 以及报错处理

    php_screw 是一个 php 源代码加密扩展.首先来看一下 php_screw 在liunx下是如何安装的 首先 去源完整下载 安装包,现在的最新版是 1.5,我们就用1.5 来做个实例 如果有 ...

  3. j2EE的web.xml详解

    https://blog.csdn.net/changqing5818/article/details/49928231 https://www.cnblogs.com/ClassNotFoundEx ...

  4. DNN 数据的表示

  5. 【python】-- Socket粘包问题 ,解决粘包的几种方法、socket文件下载,md5值检验

    上一篇随笔:“socket 接收大数据”,在win系统上能够运行,并且解决了大数据量的数据传输出现的问题,但是运行在linux系统上就会出现如下图所示的情况: 就是服务端两次发送给客户端的数据(第一次 ...

  6. amp模板展示amp网站也可以做得很好看

    ytkah比较喜欢研究一些新东西,AMP刚出来的时候就上手了,也做了一些站点,而且还不赖,因为这个还机缘巧合参加了深圳的谷歌全球合作伙伴大会,很多大牛也都来了,很荣幸能和他们一起交流.下面就稍微展示一 ...

  7. Apache Shiro 使用手册(二)Shiro 认证(转发:http://kdboy.iteye.com/blog/1154652)

    认证就是验证用户身份的过程.在认证过程中,用户需要提交实体信息(Principals)和凭据信息(Credentials)以检验用户是否合法.最常见的“实体/凭证”组合便是“用户名/密码”组合. 一. ...

  8. python基础21 ------python基础之socket编程

    一.C/S架构和B/S架构的简介 略 二.osi七层模型 略 三.socket层 1.如图所示: socket层是存在于应用层和传输层直接抽象出来的一层. 2.socket层是什么? Socket是应 ...

  9. 模仿jquery框架源码 -生长---跨域访问

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  10. python开发环境必备之vim配置

    俗话说:工欲善其事,必先利其器.最近使用python,习惯了liunx和vim,打算将vim作为python开发工具,下面就配置vim,以让它成为python开发的利器,增强我们的开发体验!废话少说, ...