剑指offer/LeetCode146/LintCode134_LRU缓存实现
来源:互联网 发布:p4vasp windows 编辑:程序博客网 时间:2024/06/05 18:35
剑指offer/LeetCode146/LintCode134_LRU缓存实现
声明
文章均为本人技术笔记,转载请注明出处:
1 https://segmentfault.com/u/yzwall
[2] blog.csdn.net/j_dark/
解题思路
LRU缓存两种功能:
get(key)
:获取key的对应value,不存在返回-1
set(key, value)
(lintcode版本)/put(key, value)
(leetcode版本):设置<key, value>
- 缓存已满,删除最近最久未被使用的节点,添加新节点进缓存
- 缓存未满,
- 节点存在,修改value;
- 节点不存在,添加新节点进缓存;
解题思路
由于LRU缓存插入和删除操作频繁,使用双向链表维护缓存节点,
+ “新节点”:凡是被访问(新建/修改命中/访问命中)过的节点,一律在访问完成后移动到双向链表尾部,保证链表尾部始终为最“新”节点;
+ “旧节点”:保证链表头部始终为最“旧”节点,LRU策略删除时表现为删除双向链表头部;
+ 从链表头部到尾部,节点访问热度逐渐递增
由于链表不支持随机访问,使用HashMap+双向链表实现LRU缓存;
+ HashMap中键值对:<key, CacheNode>
+ 双向链表:维护缓存节点CacheNode
注意点
使用双向链表时,时刻记得维护pre
和next
指针;
题目链接
- lintcode 134: http://www.lintcode.com/zh-cn/problem/lru-cache/
- leetcode 146: https://leetcode.com/problems/lru-cache/#/description
Java代码
/** * HashMap+双向链表实现LRU缓存 * http://www.lintcode.com/zh-cn/problem/lru-cache/ * https://leetcode.com/problems/lru-cache/#/description * @author yzwall */import java.util.HashMap;class Solution { private HashMap<Integer, CacheNode> map; private int capacity; // head.next和tail.next指向链表头尾,包起来防止null private CacheNode head = new CacheNode(-1, -1); private CacheNode tail = new CacheNode(-1, -1); // 缓存节点 private class CacheNode { int key, value; CacheNode pre, next; CacheNode(int key, int value) { this.key = key; this.value = value; this.pre = null; this.next = null; } } public Solution(int capacity) { this.map = new HashMap<>(); this.capacity = capacity; } // 将已有节点或新建节点移动到链表尾部 private void moveToTail(CacheNode target, boolean isNew) { // 尾部节点显然不需要移动 if (target != tail.next) { if (!isNew) { // 修改旧节点的双向链表指针 target.pre.next = target.next; target.next.pre = target.pre; } // 添加节点到链表尾部 tail.next.next = target; target.pre = tail.next; tail.next = target; } } // 命中节点添加到链表尾部,未命中返回-1 public int get(int key) { if (map.containsKey(key)) { CacheNode target = map.get(key); // 将已有节点移动到链表尾部 moveToTail(target, false); // 此时链表尾部tail.next = target,更新next指向null,防止出现环 tail.next.next = null; return target.value; } return -1; } /** * 1. 节点命中,修改节点并移动到链表尾部tail.next * 2. 节点未命中, * 2.1 cache已满,删除链表头部head.next * 2.2 cache未满,新建节点并添加到链表尾部tail.next */ public void set(int key, int value) { // cache中存在节点 if (map.containsKey(key)) { CacheNode target = map.get(key); target.value = value; map.put(key, target); // 将访问过的已有节点移动到链表尾部 moveToTail(target, false); } else if(map.size() < capacity) { // cache未满,添加节点 CacheNode newNode = new CacheNode(key, value); map.put(key, newNode); if (head.next == null) { head.next = newNode; newNode.pre = head; tail.next = newNode; } else { // 将新建节点移动到链表尾部 moveToTail(newNode, true); } } else { // cache已满,淘汰链表链表头部节点,新节点加入到链表尾部 CacheNode newNode = new CacheNode(key, value); map.remove(head.next.key); map.put(key, newNode); // cache中只有一个元素 if (head.next == tail.next) { head.next = newNode; tail.next = newNode; } else { // cache中不止一个元素,删除头部 head.next.next.pre = head; // 更新新头部.pre = head head.next = head.next.next;// 更新新头部 // 将新建节点移动到链表尾部 moveToTail(newNode, true); } } }}
阅读全文
0 0
- 剑指offer/LeetCode146/LintCode134_LRU缓存实现
- 剑指offer JavaScript实现
- LeetCode146 LRU Cache
- LeetCode146:LRU Cache
- Leetcode146: Path Sum II
- LeetCode146—LRU Cache
- leetcode146~LRU Cache
- leetcode146. LRU Cache
- (剑指offer)JAVA实现
- 剑指offer - 实现Singleton模式
- 剑指offer 代码实现 C++
- 剑指Offer之C++实现
- 剑指offer(Java实现)
- 剑指offer C语言实现
- 《剑指offer》JAVA编程实现
- 剑指offer--实现power函数
- 剑指offer题目java实现
- [剑指offer]用两个栈实现队列
- 计算之道第五场
- 在linux中添加ftp用户,并设置相应的权限
- Day4:正则表达式与文件格式化处理
- js实现字符串翻转的2种方式
- 从Instant-Run出发,谈谈Android上的热修复
- 剑指offer/LeetCode146/LintCode134_LRU缓存实现
- c#进程、定时器初步学习
- Android 初学者入门(一个最简单的应用程序)
- Hibernate框架-1对1关联
- 2017上海金马五校程序设计竞赛部分题解
- linux下误操作startx命令后,在登录界面进不去解决办法
- 【工作流】Jbpm(一)——认识jbpm
- LeetCode#19. Remove Nth Node From End of List
- 将Eclipse中的Web项目部署到Tomcat