LRU

定义

LeastRecentlyUsed
最近最久未使用,一种缓存淘汰策略

关键点

1、最大容量,put、get
2、O(1)的时间复杂度
3、上次访问的元素在第一个
在这里插入图片描述
在这里插入图片描述

import java.util.HashMap;

class LRUCache {
    public static class Node {
        Node next, pre;
        int key, val;

        Node(int k, int v) {
            //少了这两行会报空指针
            next = this;
            pre = this;
            key = k;
            val = v;
        }
    }

    HashMap<Integer, Node> map = new HashMap<>();
    int capacity;
    Node head;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        head = new Node(0, 0);
    }

    public int get(int key) {
        /**
         * 1、判断map包不包含key,
         * 2.1 不包含return -1
         * 2.2包含的话refresh放到链表前面,返回node.val
         */
        if (!map.containsKey(key)) return -1;
        Node node = map.get(key);
        refresh(node);
        return node.val;
    }

    public void put(int key, int value) {

        /**
         * 1、先判断包不包含key
         * 2.1 包含则更新node.val,然后refresh到前面
         * 2.2 不包含则map插入,然后refresh,最后判断有没有超过capacity,超过则del
         */
        if (map.containsKey(key)) {
            Node node = map.get(key);
            node.val = value;
            refresh(node);
        } else {
            Node node = new Node(key, value);
            map.put(key, node);
            refresh(node);
            if (map.size() > capacity) {
                del();
            }
        }
    }

    private void refresh(Node node) {
        /**
         * 双向链表就是 把当前节点的next 指向下一个结点
         * 下一个节点的pre指向当前节点
         */
        // 删除这个node
        node.next.pre = node.pre;
        node.pre.next = node.next;

        //把它加在前面
        node.pre = head;
        head.next.pre = node;
        node.next = head.next;
        head.next = node;
    }

    private void del() {
        Node node = head.pre;
        node.next.pre = node.pre;
        node.pre.next = node.next;
        map.remove(node.key);
    }

    public static void main(String[] args) {
        LRUCache cache = new LRUCache(2);
        cache.put(1, 1);
        cache.put(2, 2);
        System.out.println(cache.get(1));
        cache.put(3, 3);
        System.out.println(cache.get(2));
        cache.put(4, 4);
        System.out.println(cache.get(1));
        System.out.println(cache.get(3));
        System.out.println(cache.get(4));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值