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算法的缓冲类,由于cache会有频繁的读改操作,所以要有合适的数据结构来让 set 和 get 的复杂度很小,最好近 O(1)。
主要有两种思路:

一、用 Splay 实现,Splay 是棵 BST,同时在查找和修改的时候会让那个节点上浮到根节点,不过操作都是 O(log(n)) 级别的,而且有个问题,就是这棵树可能会变成一条链(正常节点都是按查询频率从上到下,所以很快,均摊小于 O(log(n)))。Splay 实现过于复杂,这里就不给出。
二、用双链表和 HashMap 实现。

链表的作用是记录节点的使用顺序。正常情况下 LRU 都是用这种做法的。
HashMap 实现用 key 找到 List 中的节点对象,找不到就在 List 中增加节点,并插入 HashMap。
按照要求得到或修改节点的 value。
修改节点的使用时间,也就是把 List 中的节点拉到 List 头部。
在第一步时如果节点个数大于可用容量,就将 List 的最后一个节点删去。
JAVA实现如下:

package oj.leetcode;

import java.util.*;

public class LRUCache {

    private int capacity;
private Node head, tail;
private HashMap<Integer, Node> keyNodeMap; public LRUCache(int capacity) {
this.capacity = capacity;
head = new Node(-1, -1);
tail = new Node(0, 0);
head.next = tail;
tail.pre = head;
this.keyNodeMap = new HashMap<Integer, Node>();
} public int get(int key) {
Node node = keyNodeMap.get(key);
if (node != null) {
moveToHead(node);
return node.value;
}
return -1;
} public void set(int key, int value) {
Node node = null;
if (keyNodeMap.containsKey(key)) {
node = keyNodeMap.get(key);
node.value = value;
} else {
node = new Node(key, value);
if (keyNodeMap.size() == capacity) {
keyNodeMap.remove(removeTail());
}
keyNodeMap.put(key, node);
}
moveToHead(node);
} private void moveToHead(Node node) {
if (node.pre != null || node.next != null) {
node.next.pre = node.pre;
node.pre.next = node.next;
}
node.next = head.next;
head.next.pre = node;
node.pre = head;
head.next = node;
} private int removeTail() {
int lastKey = -1;
if (tail.pre != head) {
Node lastNode = tail.pre;
lastKey = lastNode.key;
lastNode.pre.next = tail;
tail.pre = lastNode.pre;
lastNode = null;
}
return lastKey;
} class Node{
int key;
int value;
Node pre;
Node next;
public Node(int k, int v) {
key = k;
value = v;
}
}
}

Java for LeetCode 146 LRU Cache 【HARD】的更多相关文章

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

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

  2. leetcode 146. LRU Cache ----- java

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

  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. Java实现 LeetCode 146 LRU缓存机制

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

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

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

  7. Leetcode#146 LRU Cache

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

  8. Java for LeetCode 207 Course Schedule【Medium】

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. Java for LeetCode 072 Edit Distance【HARD】

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

随机推荐

  1. 曲线行驶s弯道技巧图解【转】

    s弯道怎么走?在走S弯的时候,最主要的就是控制车的速度,在做每个动作的时候要保持一样的速度,不要一会快一会慢的,在开的时候,因为每个人的身高,体型不一样,每个人看的点位都是不一样的,每次在开的时候要找 ...

  2. Oracle定时执行存储过程

    首先查看 SQL> show parameter job NAME                                 TYPE        VALUE-------------- ...

  3. Linux Cache Mechanism Summary(undone)

    目录 . 缓存机制简介 . 内核缓存机制 . 内存缓存机制 . 文件缓存机制 . 数据库缓存机制 1. 缓存机制简介 0x1: 什么是缓存cache 在计算机整个领域中,缓存(cache)这个词是一个 ...

  4. CF Gym 100685A Ariel

    传送门 A. Ariel time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. hihocoder1187 Divisors

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given an integer n, for all integers not larger than n, f ...

  6. 使用multi curl进行http并发访问

    curl是一款利用URL语法进行文件传输的工具,它支持多种协议,包括FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET等,我们既可以在命令行上使用它,也可以利用 libcur ...

  7. C#实现通过程序自动抓取远程Web网页信息的代码

    http://www.jb51.net/article/9499.htm 通过程序自动的读取其它网站网页显示的信息,类似于爬虫程序.比方说我们有一个系统,要提取BaiDu网站上歌曲搜索排名.分析系统在 ...

  8. easyui form 方式提交数据

    http://ldzyz007.iteye.com/blog/2067540 <form id="ff" method="post">      . ...

  9. [Angularjs]ng-class,ng-class-even,ng-class-odd

    写在前面 最近在通过angularjs将数据绑定到前端,其中也涉及到很多新的东西,一些效果还是很有必要实现的.在使用中发现ng-class,ng-class-even.ng-class-odd的使用, ...

  10. JavaScript入门培训材料(Copy至此以作备份)

    JavaScript简明学习教程 2014年5月31日 目录 一.说明... 2 二.准备知识... 2 (一)HTML. 2 (二)DOM.. 3 三.JavaScript简介... 3 四.Jav ...