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 ...
随机推荐
- wp8.1 Study11:APP里文件读写和使用XML和Json序列化
一.文件读写 1.基本操作(使用FileIO API) 这个方法在上一个stduy已经学过,那么贴出来复习下,代码如下: private async void writeTextToLocalStor ...
- 在线体验K2 BPM微信审批
“微信审批”在江湖中传言已久,但很多人依然“只闻其声,未见其人”,这传说中的手感到底有多好?今天,我们就一起来揭开它的真面目吧. 故事发生在上周六傍晚,我接到了加班电话. 晚上21:30终于加完班了, ...
- Cisco IOS basic system management command reference
absolute : to specify an absolute time for a time-range (in time-range configuration mode) no absolu ...
- java基础之 集合
一.ArrayList和Linkedlist的区别 1.ArrayList是基于数组,LinkedList基于链表实现. 对ArrayList和LinkedList而言,在列表末尾增加一个元素所花的开 ...
- java批量插入数据进数据库中
方式1: for循环,每一次进行一次插入数据. 方式2: jdbc的preparedStatement的batch操作 PreparedStatement.addBatch(); ...... Pre ...
- 传Windows 9预览版今秋发布
据ZDNet长期关注微软动态的资深人士玛丽•乔•弗利(Mary Jo Foley)称,Windows 9预览版将会在9月或者10月推出.按照这一进度,代号为“Threshold’”的Windows 9 ...
- IOS 导航栏属性设置
IOS 7 以上系统导航栏: [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; // 返回按钮颜色 [UINaviga ...
- HttpClient -- 血的教训
HttpClient -- 血的教训 千万别用httpClient 不支持httpVersion2.0 因为这个导致项目重做
- ICTCLAS50中文分词软件“Init ICTCLAS failed!”问题
if (!ICTCLAS_Init(Server.MapPath("ICTCLAS50"))) { Response.Write( ...
- Linux内存管理之地址映射
写在前面:由于地址映射涉及到各种寄存器的设置访问,Linux对于不同体系结构处理器的地址映射采用不同的方法,例如对于i386及后来的32位的Intel的处理器在页式映射时采用的是2级页表映射,而对于I ...