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. HDU 1358 简单kmp

    题目大意: 找到所有的可组成连续字符串相连的位置,和循环字符串的个数 #include <cstdio> #include <cstring> #include <alg ...

  2. WebSockets基础

    网络套接字是下一代WEB应用程序双向通信技术,它是基于一个独立的socket并且需要客户端浏览器支持HTML5. 一旦你了解了网络套接字与WEB服务器的连接,你将可以从浏览器发送数据到服务器并且可以接 ...

  3. 集成支付宝钱包支付iOS SDK的方法与经验

    流程 摘自第一个文档<支付宝钱包支付接口开发包2.0标准版.pdf> 图中的“商户客户端”就是我们的iOS客户端需要做的事情: 调用支付宝支付接口 处理支付宝返回的支付结果 在调用支付宝支 ...

  4. Visual Studio安装过程

    在这里需要先跟老师说一声抱歉,因为编写代码的愿意,我早在大一的时候就已经安装并且购买了正版的VS2013.所以今天在这里实在无法全部描述VS2013的安装过程. 然而,我所知的是,VS2013相对于我 ...

  5. JAVA工作方式

    public class Hellow { int eyes = 2; int ears = 2; int legs = 4; void run(){ //方法 System.out.println( ...

  6. html练习——个人简介

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 重拾java系列一java基础(4)

    本章主要回顾一些类的相关知识: (1)static: static 静态的: 属于类的资源, 使用类名访问.  静态属性: 只有一份的变量  静态方法: 是属于类方法, 可以使用类名直接访问. 静态方 ...

  8. UIControl的使用

    在开发当中,可能很多时候都需要做个点赞的需求,如果用按钮实现,按钮作为一个系统复合控件,外部是一个 View-->UIControl的容器, 内部包含了UILabel和UIImage,以及一些排 ...

  9. 结构体,内存,指针例题.DOC

    2015.1.30 递归函数:1.自身调用自己:2.要有结束条件!typedef 后面加分号:一般后面的重定义名加_,例如:typedef unsigned long int uint_16;结构体成 ...

  10. Magento创建configurable产品的要点

    接着上一篇用API创建可配置的产品Configurable Product说事.Magento的产品类型可分为Simple Product.Group Product.Configurable Pro ...