Java for LeetCode 146 LRU Cache 【HARD】
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】的更多相关文章
- leetcode 146. LRU Cache 、460. LFU Cache
LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...
- leetcode 146. LRU Cache ----- java
esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...
- [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LeetCode] 146. LRU Cache 近期最少使用缓存
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- Java实现 LeetCode 146 LRU缓存机制
146. LRU缓存机制 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - ...
- leetcode@ [146] LRU Cache (TreeMap)
https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...
- Leetcode#146 LRU Cache
原题地址 以前Leetcode的测试数据比较弱,单纯用链表做也能过,现在就不行了,大数据会超时.通常大家都是用map+双向链表做的. 我曾经尝试用C++的list容器来写,后来发现map没法保存lis ...
- 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 ...
- 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 ...
随机推荐
- 【HDU 5115】Dire Wolf
题意 n只狼排成一行,每次击败第i只狼需要ai+bi-1+bi+1代价,击败后,相当于出列了,与i相邻两只狼成了相邻的.求击败所有狼的最小总代价. 分析 我开始一直以为是个环TAT.. 区间dp,dp ...
- ThinkPHP多表联合查询的常用方法
1.原生查询示例: $Model = new Model(); $sql = 'select a.id,a.title,b.content from think_test1 as a, think_t ...
- Visio绘制时序图
用visio建立时序图 1.选择模版 2.常见符号 时序图创建步骤 1.确定交互过程的上下文: 2.识别参与过程的交互对象: 3.为每个对象设置生命线: 4.从初始消息开始,依次画出随后消息: 5.考 ...
- 新建maven项目
1.新建maven project 注意:勾上create a new simple project 2.填写相关信息, Grounp id为大项目名字,Artifact id为小项目的名字.注意:P ...
- MVC模式的学生信息增删改查
准备:建一个名为 userdb的数据库.建一个student表,有stuid,stuname,gender三个字段.其中stuid为主键.j加入相应的驱动包,相应的JSTL标签 先看目录结构 代码: ...
- 使用Jquery+EasyUI 进行框架项目开发案例讲解之四 组织机构管理源码分享
http://www.cnblogs.com/huyong/p/3404647.html 在上三篇文章 <使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码分享> ...
- swift项目中引入OC框架
- http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/ 非常棒的spring入门,maven,以及eclipse
http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-t ...
- DEDECMS 5.7之前版本远程SQL注入漏洞
2012/4/29 凌晨 知道创宇安全研究团队截获到最新DEDECMS SQL注入 0day,官网目前提供下载的最新版5.7也受影响,截止本告警发出时官方尚未给出补丁或解决方案,此漏洞利用简单且ded ...
- title及alt提示特效
<html> <head> <title>title及alt提示特效</title> <style type="text/css&quo ...