Description:

Design 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的缓存,即最近最后用的保留。

实现LRU缓存有几种方法,链表+HashMap结构实现,LinkedHashMap的实现(继承、组合)、FIFO实现。

具体见我的博客:

这里使用LikedHashMap的组合实现,简单高效。

 public class LRUCache {

     private int capacity;

     private java.util.LinkedHashMap<Integer, Integer> cache = new java.util.LinkedHashMap<Integer, Integer>(capacity,0.75f,true) {
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return size() > capacity;
}
}; public LRUCache(int capacity) {
this.capacity = capacity;
} public int get(int key) {
Integer res = cache.get(key);
return res==null?-1:res;
} public void set(int key, int value) {
cache.put(key, value);
}
}

网上比较好的答案代码也有上百行,时间也是几百毫秒,这样看起来JDK中的LinkedHashMap的实现还是很高效的。

在不必要的情况下最好不要重复造轮子——大神

LeetCode——LRU Cache的更多相关文章

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

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

  2. [LeetCode]LRU Cache有个问题,求大神解答【已解决】

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

  3. LeetCode:LRU Cache

    题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...

  4. LeetCode: LRU Cache [146]

    [题目] Design and implement a data structure for Least Recently Used (LRU) cache. It should support th ...

  5. LeetCode – LRU Cache (Java)

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

  6. Leetcode: LRU Cache 解题报告

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

  7. [LeetCode] LRU Cache [Forward]

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

  8. Leetcode:LRU Cache,LFU Cache

    在Leetcode上遇到了两个有趣的题目,分别是利用LRU和LFU算法实现两个缓存.缓存支持和字典一样的get和put操作,且要求两个操作的时间复杂度均为O(1). 首先说一下如何在O(1)时间复杂度 ...

  9. leetcode LRU Cache python

    class Node(object): def __init__(self,k,x): self.key=k self.val=x self.prev=None self.next=None clas ...

随机推荐

  1. MVC视图中处理Json

    /// <summary> /// 登录 /// </summary> /// <param name="value"></param&g ...

  2. Windows出现BOOT\BCD错误的解决办法

    这篇文章主要介绍了Windows出现BOOT\BCD错误的解决办法,本文讲解使用命令的方式解决这个问题,需要的朋友可以参考下 一般碰到 Boot Record Error 问题用系统盘自动修复一下就可 ...

  3. iPhone开发视频教程 Objective-C部分 (51课时)

    第一.二章  OC基础语法 iPhone开发教程 第一章 OC基础语法  iPhone开发概述-必看(1.1)http://www.apkbus.com/android-102215-1-1.html ...

  4. android中xmlns:tools属性详解

    今天读到一篇总结的非常棒的文章,写的逻辑很清晰也很实用,很少见到如此棒的文章了.就原文转发过来,我把格式给整理了一下,分享给园子里的各位朋友!好久没写博客了,就为2015年的11月留份纪念吧.希望对你 ...

  5. [转]揭秘webdriver实现原理

    转自:http://www.cnblogs.com/timsheng/archive/2012/06/12/2546957.html 通过研究selenium-webdriver的源码,笔者发现其实w ...

  6. discuz 使用阿里云OSS

    discuz 使用阿里云OSS (转)http://bbs.aliyun.com/read/239257.html 说明:我绑定了二级域名,本演示采用二级域名oss来介绍,我实际使用的是二级域名pic ...

  7. Java Split以竖线作为分隔符

    今天用到了Java中的Split函数,要以“|”作为分割符,当输入竖线时,发现出错,这个问题应该很久前就遇到过,不过太长时间就给忘了! 网上一搜,就找到了答案,这是因为split里面有两个参数,其中一 ...

  8. [AX 2012] Woker user request

    在HR模块和System administrator模块下都能找到Woker user request这个功能,它的作用是为员工创建一个AX账号.比如我们创建一个这样的user request: 注意 ...

  9. Java的wait(), notify()和notifyAll()使用小结

    wait(),notify()和notifyAll()都是java.lang.Object的方法: wait(): Causes the current thread to wait until an ...

  10. golang append

    1) Append a slice b to an existing slice a: a = append(a, b...) 2) Copy a slice a to a new slice b: ...