剑指offer/LeetCode146/LintCode134_LRU缓存实现

剑指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

注意点

使用双向链表时,时刻记得维护prenext指针;

题目链接

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值