leetcode 146. LRU Cache ----- java
esign 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即Least Recently Used 近期最少使用算法。
1、使用了HashMap和ArrayList
public class LRUCache {
int size,num;
List<Integer> list ;
Map<Integer,Integer> map;
public LRUCache(int capacity) {
size = capacity;
num = 0;
list = new ArrayList<Integer>();
map = new HashMap<Integer,Integer>();
}
public int get(int key) {
if( map.containsKey(key) ){
list.remove((Integer)key);
list.add(key);
return map.get(key);
}
else
return -1;
}
public void set(int key, int value) {
if( map.containsKey(key) ){
list.remove((Integer)key);
map.put(key,value);
list.add(key);
}else{
if( num == size ){
map.remove(list.get(0));
list.remove((int)0);
map.put(key,value);
list.add(key);
}else{
map.put(key,value);
list.add(key);
num++;
}
}
}
}
2、使用LinkedHashMap.
public class LRUCache {
int size,num;
Map<Integer,Integer> map;
public LRUCache(int capacity) {
size = capacity;
List list =new LinkedList();
map = new LinkedHashMap<Integer,Integer>();
}
public int get(int key) {
if( map.containsKey(key) ){
int value = map.get(key);
map.remove(key);
map.put(key,value);
return value;
}
else
return -1;
}
public void set(int key, int value) {
if( map.containsKey(key) ){
map.remove(key);
map.put(key,value);
}else{
if( num == size ){
int firstKey = map.keySet().iterator().next();
map.remove(firstKey);
map.put(key,value);
}else{
map.put(key,value);
num++;
}
}
}
}
3、可以使用双向链表实现。
leetcode 146. LRU Cache ----- java的更多相关文章
- leetcode 146. LRU Cache 、460. LFU Cache
LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...
- Java for LeetCode 146 LRU Cache 【HARD】
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LeetCode] 146. LRU Cache 近期最少使用缓存
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- leetcode@ [146] LRU Cache (TreeMap)
https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...
- Leetcode#146 LRU Cache
原题地址 以前Leetcode的测试数据比较弱,单纯用链表做也能过,现在就不行了,大数据会超时.通常大家都是用map+双向链表做的. 我曾经尝试用C++的list容器来写,后来发现map没法保存lis ...
- Java实现 LeetCode 146 LRU缓存机制
146. LRU缓存机制 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - ...
- 【LeetCode】146. LRU Cache 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+双向链表 日期 题目地址:https://le ...
- LeetCode – LRU Cache (Java)
Problem Design and implement a data structure for Least Recently Used (LRU) cache. It should support ...
随机推荐
- WebSockets基础
网络套接字是下一代WEB应用程序双向通信技术,它是基于一个独立的socket并且需要客户端浏览器支持HTML5. 一旦你了解了网络套接字与WEB服务器的连接,你将可以从浏览器发送数据到服务器并且可以接 ...
- php变量的判空和类型判断
(1)var_dump(); 判断一个变量是否已经声明并且赋值,并且打印类型和值 <?php $a; var_dump($a);//输出null <?php var_dump($a);// ...
- MapReduce 重要组件——Recordreader组件
(1)以怎样的方式从分片中读取一条记录,每读取一条记录都会调用RecordReader类: (2)系统默认的RecordReader是LineRecordReader,如TextInputFormat ...
- 速度!!!抢KIS英文版(多设备版)3年激活码
活动地址 http://promo.kaspersky.com/wvu直接打不开,需要用 US 的 在 线 代 理,暂时提供1个(204.12.228.235)可以绕过第一步邮箱验证注意:3年版多设备 ...
- oracle 删除表和数据分析语句
TRUNCATE TABLE 表名;ANALYZE TABLE 表名 ESTIMATE STATISTICS;
- 2016 - 1- 19 利用多线程优化从网上加载图片的Demo
// // ZZTableViewController.m // 多图片下载 // // Created by Mac on 16/1/19. // Copyright © 2016年 Mac. Al ...
- META标签的NAME变量
META标签的NAME变量语法格式是: <META NAME=xxx CONTENT=xxxxxxxxxxxxxxxxxx> 其中xxx主要有下面几种参数: 1. Keywords(关键字 ...
- 网络图片下载缓存库SDWebImage的使用
SDWebImage导入问题 最新的SDWebImage由于是基于ARC模式写的,如果创建的是非ARC醒目的童鞋们注意,导入文件夹之后,先添加ImageIO.framework,mapKit.fram ...
- Interview----将一棵二叉树转换成其镜像
题目:输入一颗二元查找树,将该树转换为它的镜像, 即在转换后的二元查找树中,左子树的结点都大于右子树的结点. 用递归和循环两种方法完成树的镜像转换. 例如输入: 8 / ...
- ubuntu下修改mysql默认字符编码出现的Job failed to start解决办法
ubuntu下修改mysql默认字符编码出现的Job failed to start解决办法 前几天卸掉了用了好多年的Windows,安装了Ubuntu12.04,就开始各种搭环境.今天装好了MySQ ...