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的更多相关文章

  1. leetcode 146. LRU Cache 、460. LFU Cache

    LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...

  2. Java for LeetCode 146 LRU Cache 【HARD】

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

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

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

  4. [LeetCode] 146. LRU Cache 近期最少使用缓存

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

  5. leetcode@ [146] LRU Cache (TreeMap)

    https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...

  6. Leetcode#146 LRU Cache

    原题地址 以前Leetcode的测试数据比较弱,单纯用链表做也能过,现在就不行了,大数据会超时.通常大家都是用map+双向链表做的. 我曾经尝试用C++的list容器来写,后来发现map没法保存lis ...

  7. Java实现 LeetCode 146 LRU缓存机制

    146. LRU缓存机制 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - ...

  8. 【LeetCode】146. LRU Cache 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+双向链表 日期 题目地址:https://le ...

  9. LeetCode – LRU Cache (Java)

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

随机推荐

  1. Js中 关于top、clientTop、scrollTop、offsetTop的用法

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  2. iphone判断当前网络连接类型

    eachability只能区分出无网络.wifi和wwan(2G&2.5G&3G)类型的网络连接类型,只需重构networkStatusForFlags方法,即可详细区分出2G与3G网 ...

  3. 简单的两数之和再次乱入<< Add Two Numbers >>

    请看题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored ...

  4. 用Unity实现的依赖注入

    第一步:添加引用 上面那两个玩意 第二步:在app_start中添加unityconfig 上面那个玩意 第三步:在global.asax文件中进行初始化 如上面的,在application_star ...

  5. 2016 - 1- 22 NSURLConnetction --- POST请求

    一:与上一篇博客中的GET方法类似  只不过需要多注意,如果要改变请求的类型,需要生成NSMutableURLRequest对象才可以设置请求的类型. NSURL *url = [NSURL URLW ...

  6. hadoop环境搭建遇到问题集锦

    1  在hadoop的bin目录下, 运行hadoop version命令,提示“hadoop:没有此命令” 解决办法: ./hadoop version或者$HADOOP_HOME/bin放在PAT ...

  7. Git ~ 添加远程仓库 ~Git

    现在的情景是 , 你已经在本地创建了一个Git仓库后 , 又想在 Github 创建一个Git 仓库并且让这两个仓库进行远程同步 , 这样Github 上的仓库既可以作为备份 ,有可以让其他人通过仓库 ...

  8. 在国内时,更新ADT时需要配置的

    RT

  9. linux 任务调度 系统任务调度

    linux  at 针对运行一次的任务 crontab   控制计划任务的命令 crond系统服务 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程, 与windows ...

  10. magento 去掉index.php .html

    首先应该确保你的服务器环境,达到了magento的运行需求,若你是使用的是magento专用空间就不必担心了,若不是请查看我以前的文章,检测你的运行环境(http://www.hellokeykey. ...