(重要)LRU cache
[抄题]:
[思维问题]:
需要从任何位置访问某数字有没有(重要 ),返回其位置(不重要),所以用hashmap。
需要从任何位置删除,用linkedlist。最终二者结合,用linked hashmap。
[一句话思路]:
链表存物理位置,key存数,value存值(要更新)
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- prev next都是node类型。node第一次初始化的时候没有参数key value public时有.Node head = new Node(-1,-1)前后都是node,类型保持一致.初始节点的prev next都是null
- LRU中的初始化是把head,tail的中间部分连接起来
- 私有化数据类型的时候要有hashmap,因为有点,可以直接把key对应的点取出来.长度用.size()表示
- 是否存在用containsKey
- move_to_tail(Node current) 里面有参数. 有get就不用移动,没get就要移动
- set中如果已经存在,判断条件是if (get(key) != -1),而不是containskey就行了,因为还要放到最后一位
- 满了的时候,remove的是head.next!!!因为head是空的。先把head.next.key值删remove了再说。head.next = head.next.next;指定指针 head.next.prev = head;在前一步的基础上指定指针
- insert是新建的节点, 要写new
- hashmap的操作是put, get。queue的操作语言是poll, add。stack是push/pop。(pgh,qpa,spp)
- move_2_end中,从左到右,节点的操作顺序是1432
[二刷]:
- private私有类是小写,而且要加上class
- get函数记得写返回语句
- hs满了之后,要写remove head.next的key,让它不被找到
- Node insert = new Node(key,value)直接指定了,加入hs中的动作是put(key,insert)(因为是这么定义的)
- 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的更多相关文章
- [LeetCode] LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 【leetcode】LRU Cache
题目简述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support t ...
- LeetCode:LRU Cache
题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...
- LRU Cache实现
最近在看Leveldb源码,里面用到LRU(Least Recently Used)缓存,所以自己动手来实现一下.LRU Cache通常实现方式为Hash Map + Double Linked Li ...
- 【leetcode】LRU Cache(hard)★
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LintCode] LRU Cache 缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- LRU Cache [LeetCode]
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 43. Merge Sorted Array && LRU Cache
Merge Sorted Array OJ: https://oj.leetcode.com/problems/merge-sorted-array/ Given two sorted integer ...
- LeetCode——LRU Cache
Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...
- LRU Cache
LRU Cache 题目链接:https://oj.leetcode.com/problems/lru-cache/ Design and implement a data structure for ...
随机推荐
- php文件下载(解决文件下载后多几个字节的问题) 与封装成类的例子
php文件下载比较常见,网上的资料比较多,在此不再强调怎么去实现(因为也是网上看的).下面主要说明的是下载代码的注意点. php下载文件主要是把文件以字节流直接输出,也就是echo fread($fi ...
- Unreal Engine 4 反射机制的实现
转自:http://blog.csdn.net/noahzuo/article/details/51482255 反射机制 反射机制指的是程序可以在运行期间进行检测和修改自己状态的能力. UE4引擎使 ...
- Spark 编程模型(下)
创建Pair RDD 什么是Pair RDD 创建Pair RDD Pair RDD的转化操作 Pair RDD的转化操作1 在xshell启动 reduceByKey的意思是把相同的key的valu ...
- 图文并茂 RAID 技术全解 – RAID0、RAID1、RAID5、RAID100
RAID 技术相信大家都有接触过,尤其是服务器运维人员,RAID 概念很多,有时候会概念混淆.这篇文章为网络转载,写得相当不错,它对 RAID 技术的概念特征.基本原理.关键技术.各种等级和发展现状进 ...
- gz文件最后四位检测
[root@node-0 ~]# ll -rw-r--r-- 1 root root 24048 Nov 29 11:29 install.log 文件大小为24048 [root@node-0 ~ ...
- 利用goole guava 下载文件到本地
package com.road.crawler.meizitu.crawler; import java.io.File; import java.io.IOException; import ja ...
- Git 代码版本还原方法
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,Git 代码版本还原方法 在使用 Git 管理自己的代码和资料时,难免会遇到意料 ...
- mysql 数据字典
Information_schema数据库是MySQL自带的,它提供了访问数据库元数据的方式. desc tb_name ,describe tb_name,show columns from tb_ ...
- 浅谈RMI的特点及作用
RMI:远程方法调用(Remote Method Invocation) 扩展:RPC与RMI的区别 1:方法调用方式不同: RMI中是通过在客户端的Stub对象作为远程接口进行远程方法的调用.每个远 ...
- UI5-文档-4.5-Controllers
在这个步骤中,我们将文本替换为一个按钮,并在按钮被按下时显示“Hello World”消息.按钮的按下事件的处理是在视图的控制器中实现的. Preview A Say Hello button is ...