题目

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

    LRU是Least Recent Used的缩写,即最少使用的Cache置换算法。是为虚拟页式存储管理服务的。Cache是快速缓存。这是IT行业常常见到的概念。CPU中的Cache能极大提高存取指令和数据的时间,让整个存储器(Cache+内存)既有Cache的快速度,又有内存的大容量。
    Cache尽管速度快,可是容量有限。因此当Cache容量用完而又有新的内容加入进来的时候,就须要选取Cache中的部分内容进行舍弃,然后加入新的内容。LRU Cache的替换规则是:每次选取最久不被使用的内容进行舍弃,然后加入新的内容。之前上操作系统,老师告诉我的LRU Cache Algorithm中文翻译是近期最久未使用算法。

思路

    LRU的典型实现是double linked list + hash map。

原理是:

  1. 双向链表根据每一个节点近期被訪问的时间有序存储,近期被訪问的节点存储在表头,近期没有被訪问的节点存储的表尾,存储根据是由于:近期被訪问的节点在接下来的一段时间仍有非常大的概率被再次訪问到。

  2. 哈希表的作用是用来提高查找效率,假设不使用哈希表。则查找一个节点的时间复杂度是O(n)。而使用了哈希表,则每一个节点的查找时间复杂度为O(1)。
    查找(GET)操作:
  • 依据键值查找hashmap,假设没找到直接返回-1
  • 若找到相应节点node,则将其插入到双向链表表头
  • 返回node的value值
    插入(SET)操作:
  • 依据键值查找hashmap。假设找到。则直接将该节点移到表头就可以
  • 假设没有找到。首先推断当前Cache是否已满
  • 假设已满,则删除表尾节点
  • 将新节点插入到表头

AC代码

    AC代码例如以下,写的比較乱,事实上非常多方法是能够简洁复用的,可是这里之所以不改,是为了让大家更好的清楚LRU的流程和原理:
import java.util.HashMap;

public class LRUCache {
private HashMap<Integer, DoubleListNode> mHashMap;
private DoubleListNode head;
private DoubleListNode tail;
private int capacity;
private int currentsize; public LRUCache(int capacity) {
this.capacity = capacity;
this.currentsize = 0;
this.mHashMap = new HashMap<Integer, DoubleListNode>();
this.head = this.tail = null;
} public int get(int key) {
if (mHashMap.containsKey(key)) {
DoubleListNode tNode = mHashMap.get(key);
if (tNode == tail) {
if (currentsize > 1) {
removeNodeFromTail();
moveNodeToHead(tNode);
}
} else if (tNode == head) {
// do nothing
} else {
tNode.pre.next = tNode.next;
tNode.next.pre = tNode.pre;
moveNodeToHead(tNode);
}
return mHashMap.get(key).value;
} else {
return -1;
}
} private void removeNodeFromTail() {
tail = tail.pre;
if (tail != null) {
tail.next = null;
}
} private void moveNodeToHead(DoubleListNode node) {
head.pre = node;
node.next = head;
node.pre = null;
head = node;
} public void set(int key, int value) {
if (mHashMap.containsKey(key)) {
// 更新HashMap中相应的值,并将key相应的Node移至队头
DoubleListNode tNode = mHashMap.get(key);
tNode.value = value;
if (tNode == tail) {
if (currentsize > 1) {
removeNodeFromTail();
moveNodeToHead(tNode);
}
} else if (tNode == head) {
// do nothing
} else {
tNode.pre.next = tNode.next;
tNode.next.pre = tNode.pre;
moveNodeToHead(tNode);
} mHashMap.put(key, tNode);
} else {
DoubleListNode node = new DoubleListNode(key, value);
mHashMap.put(key, node);
if (currentsize == 0) {
head = tail = node;
currentsize += 1;
} else if (currentsize < capacity) {
moveNodeToHead(node);
currentsize += 1;
} else {
// 删除tail节点。而且添加一个head节点
mHashMap.remove(tail.key);
removeNodeFromTail(); // 添加头节点
moveNodeToHead(node);
}
}
} public static void main(String[] args) {
LRUCache lruCache = new LRUCache(1);
lruCache.set(2, 1);
System.out.println(lruCache.get(2));
lruCache.set(3, 2);
System.out.println(lruCache.get(2));
System.out.println(lruCache.get(3));
} private static class DoubleListNode {
public DoubleListNode pre;
public DoubleListNode next;
public int key;
public int value; public DoubleListNode(int key, int value) {
this.key = key;
this.value = value;
this.pre = this.next = null;
}
} }

LRU算法&amp;&amp;LeetCode解题报告的更多相关文章

  1. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  2. 2021字节跳动校招秋招算法面试真题解题报告--leetcode206 反转链表,内含7种语言答案

    206.反转链表 1.题目描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1-> ...

  3. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  4. 2021字节跳动校招秋招算法面试真题解题报告--leetcode19 删除链表的倒数第 n 个结点,内含7种语言答案

    2021字节跳动校招秋招算法面试真题解题报告--leetcode19 删除链表的倒数第 n 个结点,内含7种语言答案 1.题目描述 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点. ...

  5. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  6. LeetCode解题报告:LRU Cache

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

  7. LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku

    1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...

  8. LeetCode解题报告—— 1-bit and 2-bit Characters & 132 Pattern & 3Sum

    1. 1-bit and 2-bit Characters We have two special characters. The first character can be represented ...

  9. leetCode解题报告5道题(六)

    题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...

随机推荐

  1. Mac 配置java版本 ---- MySql数据库权限设置 --- openfire

    java -version 显示java 版本 sudo su - root 切换身份 cd /usr/local/openfire 进入openfire目录 cd bin/ 进入 bin vim o ...

  2. Uva 1354 Mobile Computing

    题目链接 题意: 在一个宽为r 的房间里, 有s个砝码, 每个天平的一端要么挂砝码, 要么挂另一个天平, 并且每个天平要保持平衡. 求使得所有砝码都放在天平上, 且总宽度不超过房间宽度的最大值. 思路 ...

  3. 最浅显、易懂的Linux 硬链接与软链接的理解

    正文: Linux上的文件可以这么理解:文件-->文件名.文件是一个Object,也就是磁盘上的二进制数据.一个文件可以有多个文件名,平时我们都是通过文件名访问文件Object. 这样,硬链接可 ...

  4. BZOJ 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路

    Description 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛.一 张藏宝图上说,如果他的路程上经过的小岛依次出 ...

  5. uva 1396 - Most Distant Point from the Sea

    半平面的交,二分的方法: #include<cstdio> #include<algorithm> #include<cmath> #define eps 1e-6 ...

  6. Spring MVC注解冲突

    SpringMVC+MyBatis - 7 Spring自动扫描注解类的冲突问题 http://www.blogjava.net/crazycy/archive/2014/07/12/415738.h ...

  7. Dynamips/Dynagen模拟CISCO路由环境

    今天将<网络互连技术>--路由,交换与远程访问实训教程的实验书拿出来了看了部门. 搭建了一个基于DYNAGEN的虚拟环境. 归纳一下大约步骤: ~~~~~~~~~~~~~~ 一,在WIND ...

  8. layer.js:2 Uncaught TypeError: Cannot read property 'extend' of undefined

    在引用layer.js插件进行前端编程的时候,如果报这个错,解决办法只需: 把layer的引用放在有冲突的js库前面就行了

  9. Seattle(65) lypzxy的博客

    http://www.cnblogs.com/cb168/tag/Firemonkey/

  10. Perl,Python,Ruby,Javascript 四种脚本语言比较

    Perl 为了选择一个合适的脚本语言学习,今天查了不少有关Perl,Python,Ruby,Javascript的东西,可是发现各大阵营的人都在吹捧自己喜欢的语言,不过最没有争议的应该是Javascr ...