一、LRU缓存机制(LeetCode-146)

1.1 题目描述

1.2 解题思路

思路1:

使用Map存放key,value,使用List存放key和count,count为最新的index值,每次put、get操作都会使index自增。

进行put操作时,如果发现超过容量值capacity,则对list中的count排序,map和list都删除掉index最小的元素。(提示超时)

思路2:

使用LinkedList,每次put操作或get操作,当list中没有该key的元素的时候,且不超过容量时,直接插入元素,若有则删除key对应的原有元素,插入key对应的新元素值。

如果超过容量,则删除第一个元素,再添加进去。(通过)

1.3 解题代码

思路1:


public class LRUCache { private Map<Integer, Integer> map = null;
private List<HitCount> list = null;
private Map<Integer, HitCount> locationMap = null;
private int index = 0;
private int capacity = 0; public LRUCache(int capacity) {
map = new HashMap<>(capacity);
list = new LinkedList<>();
locationMap = new HashMap<>(capacity);
this.capacity = capacity;
} public int get(int key) {
//先找到key-value
Integer value = map.get(key);
if (value == null) {
return -1;
} HitCount h = locationMap.get(key);
h.setCount(++index);
return value;
} public void put(int key, int value) {
//若key已存在
Integer existValue = map.get(key);
//容量不充足
if (existValue == null && map.size() == capacity) {
//找到命中次数最少的一个、若命中次数相同,则去除插入最早的
HitCount leastKey = getLeastKey();
map.remove(leastKey.getKey());
list.remove(leastKey);
locationMap.remove(leastKey.getKey());
}
HitCount h = null;
if (existValue != null) {
h = locationMap.get(key);
h.setCount(++index);
} else {
h = new HitCount(key, ++index);
list.add(h);
}
map.put(key, value);
locationMap.put(key, h);
index++;
} private HitCount getLeastKey() {
list = list.stream().sorted((u1, u2) -> (u1.getCount() - u2.getCount())).collect(Collectors.toList());
return list.get(0);
} class HitCount {
private int key;
private int count; HitCount(int key, int count) {
this.key = key;
this.count = count;
} public int getKey() {
return key;
} public void setKey(int key) {
this.key = key;
} public int getCount() {
return count;
} public void setCount(int count) {
this.count = count;
} } }

思路2:


public class LRUCache { private List<LRUMap> list = null;
private int capacity = 0; public LRUCache(int capacity) {
list = new LinkedList<>();
this.capacity = capacity;
} public int get(int key) {
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
LRUMap node = (LRUMap) iterator.next();
if (node.getKey() == key) {
int value = node.getValue();
list.remove(node);
list.add(new LRUMap(key, value));
return value;
}
}
return -1;
} public void put(int key, int value) {
LRUMap node = new LRUMap(key, value); //查看该节点是否存在
if (list.contains(node)) {
list.remove(node);
}
//如果超过容量
if (list.size() == capacity) {
list.remove(0);
}
list.add(node); } class LRUMap { private int key;
private int value; public LRUMap(int key, int value) {
this.key = key;
this.value = value;
} public int getKey() {
return key;
} public void setKey(int key) {
this.key = key;
} public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
} @Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
} LRUMap map = (LRUMap) obj;
return key == map.key;
}
} }

LeetCode设计实现题(一)的更多相关文章

  1. 【python】Leetcode每日一题-设计停车系统

    [python]Leetcode每日一题-设计停车系统 [题目描述] 请你给一个停车场设计一个停车系统.停车场总共有三种不同大小的车位:大,中和小,每种尺寸分别有固定数目的车位. 请你实现 Parki ...

  2. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  3. 【js】Leetcode每日一题-完成所有工作的最短时间

    [js]Leetcode每日一题-完成所有工作的最短时间 [题目描述] 给你一个整数数组 jobs ,其中 jobs[i] 是完成第 i 项工作要花费的时间. 请你将这些工作分配给 k 位工人.所有工 ...

  4. 【python】Leetcode每日一题-笨阶乘

    [python]Leetcode每日一题-笨阶乘 [题目描述] 通常,正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积.例如,factorial(10) = 10 * 9 * 8 * 7 * 6 ...

  5. 【python】Leetcode每日一题-132模式

    [python]Leetcode每日一题-132模式 [题目描述] 给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j &l ...

  6. 【python】Leetcode每日一题-扁平化嵌套列表迭代器

    [python]Leetcode每日一题-扁平化嵌套列表迭代器 [题目描述] 给你一个嵌套的整型列表.请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数. 列表中的每一项或者为一个整数,或者是另 ...

  7. leetcode第37题--Count and Say

    题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...

  8. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  9. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

随机推荐

  1. 【MySQL】数据库(分库分表)中间件对比

    分区:对业务透明,分区只不过把存放数据的文件分成了许多小块,例如mysql中的一张表对应三个文件.MYD,MYI,frm. 根据一定的规则把数据文件(MYD)和索引文件(MYI)进行了分割,分区后的表 ...

  2. js实现图片的Blob base64 ArrayBuffer 的各种转换

    一.相关基础知识 构造函数 FileReader() 返回一个新构造的FileReader 事件处理 FileReader.onabort  处理abort事件.该事件在读取操作被中断时触发. Fil ...

  3. Modelsim问题集锦

    前言 收集工程调试中遇到的modelsim问题. 问题 (1)仿真发现时钟信号和理论上的数据信号没有边沿对齐. 解决:一般是时钟精度不匹配的问题. 如果想要1ns的精度则代码中的精度需设置为: v语法 ...

  4. 【数字图像处理】gamma变换

    论文:gamma校正的快速算法及其c语言实现 gamma变换实现过程 假设图像中有一个像素,值是 200 ,那么对这个像素进行校正必须执行如下步骤: 1. 归一化 :将像素值转换为  0 - 1  之 ...

  5. bootstrap table分页limit计算pageIndex和pageSize

    由于bootstrap table的js无法直接获取pageSize和pageIndex的值,只能通过limit进行计算.

  6. WCF Restful Service

    对 Web Services.WCF 和 Restful 的扫盲可参见:https://www.cnblogs.com/scy251147/p/3382436.html 关于之前对 WCF 的学习,可 ...

  7. sqlalchemy.exc.CompileError: (in table 'user', column 'username'): VARCHAR requires a length on dialect mysql

    映射数据库时报错:sqlalchemy.exc.CompileError: (in table 'user', column 'username'): VARCHAR requires a lengt ...

  8. -bash: zip: command not found提示解决办法

    -bash: zip: command not found是因为liunx服务器上没有安装zip命令,需要安装一下即可linux安装zip命令:apt-get install zip 或yum ins ...

  9. Android-jacoco代码覆盖率:单元测试覆盖率+功能测试覆盖率

    参考:https://docs.gradle.org/current/dsl/org.gradle.testing.jacoco.tasks.JacocoCoverageVerification.ht ...

  10. Selenium(十三)调用js,控制浏览器的滚动条

    WebDiver 不能操作本地 Windows 控件,但对于浏览器上的控件也不是都可以操作的.比如浏览器的滚动条,虽然 WebDriver 提供操作浏览器的前进和后退按钮,但对于滚动条并没有提供相应用 ...