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. 图解Android - System Service 概论 和 Android GUI 系统

    通过 图解Android - Binder 和 Service 一文中,我们已经分析了Binder 和 Service的工作原理.接下来,我们来简要分析Android 系统里面都有哪些重要的Servi ...

  2. 【转】从Go、Swift语言出发

    Google于2009年第一次提出了Go的构思,Facebook在去年春天引入了Hack,随后不久Apple也发布了其Swift语言. 在战争中,胜利者写历史书:在科技中,赢的公司都在写编程语言.互联 ...

  3. Project facet Java version 1.8 is not supported解决记录

    一看知道是因为jdk版本不一致所导致,如何解决? 方法一: 选中项目 Properties , 选择 Project Facets,右击选择 Java , Change Version  方法二: 在 ...

  4. JAVA输入一个整数,求出其所有质因数

    首先得求出能整除A的数,再判断I是否是质数!!! import java.util.*; public class aa { public static void main(String[] args ...

  5. Android Studio学习笔记

    转:http://stormzhang.com/devtools/2014/11/25/android-studio-tutorial1 背景 相信大家对Android Studio已经不陌生了,An ...

  6. java初学的分析

    java初学的分析第一阶段:入门阶段学习目标:简单项目开发学习内容:1.Java入门书籍,Java基础知识.关于Java入门级的书,给大家推荐过<Java编程思想>.<Java核心技 ...

  7. mysql 存储过程中注意的问题

    OPEN cur; REPEAT FETCH cur INTO int_type, int_element_id, int_num, int_user_id; IF NOT _DONE THEN IF ...

  8. 使用Adivisor配置增强处理

    使用Adivisor配置增强处理 实现步骤: 1.通过MethodBeforeAdivice接口实现前置增强处理 public class ServiceBeforeAdvisor implement ...

  9. IHttpModule在webconfig中的注册

    在asp.net中,提供了两种方式用来解决获取由asp.net服务器创建和维护的HttpApplication对象,方便注册HttpApplication对象的事件处理.这两种方式为:IHtpModu ...

  10. Spring常用annotation标签

    @Service @Scope @Transactional @Autowired @Qualifier @PostConstruct @PreDestroy