[抄题]:

[思维问题]:

需要从任何位置访问某数字有没有(重要 ),返回其位置(不重要),所以用hashmap。

需要从任何位置删除,用linkedlist。最终二者结合,用linked hashmap。

[一句话思路]:

链表存物理位置,key存数,value存值(要更新)

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. prev next都是node类型。node第一次初始化的时候没有参数key value public时有.Node head = new Node(-1,-1)前后都是node,类型保持一致.初始节点的prev next都是null
  2. LRU中的初始化是把head,tail的中间部分连接起来
  3. 私有化数据类型的时候要有hashmap,因为有点,可以直接把key对应的点取出来.长度用.size()表示
  4. 是否存在用containsKey
  5. move_to_tail(Node current) 里面有参数. 有get就不用移动,没get就要移动
  6. set中如果已经存在,判断条件是if (get(key) != -1),而不是containskey就行了,因为还要放到最后一位
  7. 满了的时候,remove的是head.next!!!因为head是空的。先把head.next.key值删remove了再说。head.next = head.next.next;指定指针 head.next.prev = head;在前一步的基础上指定指针
  8. insert是新建的节点, 要写new
  9. hashmap的操作是put, get。queue的操作语言是poll, add。stack是push/pop。(pgh,qpa,spp)
  10. move_2_end中,从左到右,节点的操作顺序是1432

[二刷]:

  1. private私有类是小写,而且要加上class
  2. get函数记得写返回语句
  3. hs满了之后,要写remove head.next的key,让它不被找到
  4. Node insert = new Node(key,value)直接指定了,加入hs中的动作是put(key,insert)(因为是这么定义的)
  5. hs.get(key) 得到的是一个点,get(key)得到的是一个数

[三刷]:

[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构,为什么不用别的数据结构]:

链表存物理位置,key存数,value存值(要更新)

[其他解法]:

[Follow Up]:

[题目变变变]:

public class LRUCache {
/*
* @param capacity: An integer
*/
private class Node {
Node prev;
Node next;
int key;
int value; public Node(int key,int value){
this.key = key;
this.value = value;
this.prev = null;
this.next = null;
}
} private HashMap<Integer,Node> hs = new HashMap<Integer,Node>();
private int capacity;
private Node head = new Node(-1,-1);
private Node tail = new Node(-1,-1); public LRUCache(int capacity) {
this.capacity = capacity;
head.next = tail;
tail.prev = head;
} /*
* @param key: An integer
* @return: An integer
*/
public int get(int key) {
if (!hs.containsKey(key)) {
return -1;
}
//remove
Node curt = hs.get(key);
curt.prev.next = curt.next;
curt.next.prev = curt.prev;
//move_to_tail
move_to_tail(curt); return hs.get(key).value;
} /*
* @param key: An integer
* @param value: An integer
* @return: nothing
*/
public void set(int key, int value) {
if (get(key) != -1) {
hs.get(key).value = value;
return ;
}
//if full,remove head.next
if (hs.size() == capacity) {
hs.remove(head.next.key);
head.next = head.next.next;
head.next.prev = head;
}
//insert,move_to_tail
Node insert = new Node(key,value);
hs.put(key,insert);
move_to_tail(insert);
} private void move_to_tail(Node curt){
curt.prev = tail.prev;
tail.prev = curt;
curt.prev.next = curt;
curt.next = tail;
}
}

(重要)LRU cache的更多相关文章

  1. [LeetCode] LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  2. 【leetcode】LRU Cache

    题目简述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support t ...

  3. LeetCode:LRU Cache

    题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...

  4. LRU Cache实现

    最近在看Leveldb源码,里面用到LRU(Least Recently Used)缓存,所以自己动手来实现一下.LRU Cache通常实现方式为Hash Map + Double Linked Li ...

  5. 【leetcode】LRU Cache(hard)★

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  6. [LintCode] LRU Cache 缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  7. LRU Cache [LeetCode]

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  8. 43. Merge Sorted Array && LRU Cache

    Merge Sorted Array OJ: https://oj.leetcode.com/problems/merge-sorted-array/ Given two sorted integer ...

  9. LeetCode——LRU Cache

    Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...

  10. LRU Cache

    LRU Cache 题目链接:https://oj.leetcode.com/problems/lru-cache/ Design and implement a data structure for ...

随机推荐

  1. [UE4]acotor放置4*4列表

    // Number of blocks const int32 NumBlocks = Size * Size; // Loop to spawn each block ; BlockIndex< ...

  2. linux上很方便的上传下载文件工具rz和sz使用介绍

    简单说就是,可以很方便地用这两个sz/rz工具,实现Linux下和Windows之间的文件传输(发送和接收),速度大概为10KB/s,适合中小文件.rz/sz 通过Zmodem协议传输数据   一般来 ...

  3. redis 导出

    1 安装redis: apt update apt-get install redis-server 2: cd /etc/redis 3:  用redis-cli导出key redis-cli -h ...

  4. bitmap运算

    概述 所谓bitmap就是用一个bit位来标记某个元素对应的value,而key即是这个元素.由于采用bit为单位来存储数据,因此在可以大大的节省存储空间     算法思想 32位机器上,一个整形,比 ...

  5. storm的可靠性

    消息确认机制: 在数据发送的过程中可能会数据丢失导致没能接收到,spout有个超时时间(默认是30S),如果30S过去了还是没有接收到数据,也认为是处理失败. 运行结果都是处理成功 参考代码Storm ...

  6. Redis安装测试

    1.在线下载,redis是C语言开发的,编译需要依赖一个gcc的环境: yum install gcc-c++,需要保证网络畅通,在线安装: 键入y 环境安装完成后,接下来安装redis; 可以先去h ...

  7. Executor框架(一)Executor框架介绍

    Executor框架简介 Executor框架的两级调度模型   在HotSpot VM的线程模型中,Java线程被一对一映射为本地操作系统线程.Java线程启动时会创建一个本地操作系统线程:当Jav ...

  8. K-means算法(理论+opencv实现)

    写在前面:之前想分类图像的时候有看过k-means算法,当时一知半解的去使用,不懂原理不懂使用规则...显然最后失败了,然后看了<机器学习>这本书对k-means算法有了理论的认识,现在通 ...

  9. VS2015+Opencv半永久配置

    电脑W7 64位+VS2015+opencv3.0 刚开始学习opeencv很麻烦,配置的问题都弄了好久,一旦重装又出现很多问题,在网上看了一个论坛说的永久配置,特意记录一下! 第一步:下载openc ...

  10. webserver有哪些

    http://blog.csdn.net/mfsh_1993/article/details/70245380 常用web服务器有Apache.Nginx.Lighttpd.Tomcat.IBM We ...