LRU算法简介及其代码实现

LRU(Least Recently Used):最近最少使用算法简介及其代码实现(双向链表+HashMap)

简介

  • 新增元素时,若达到最大容量,则淘汰最近最少使用的元素。
  • 淘汰规则示意如下图,假设最大容量为3:
    LRU示意

双向链表 + HashMap 实现LRU算法(Java)

  • get()和put()操作的时间复杂度均为o(1)
  • get(key)操作
    • 若map.get(key)为空,则返回null;
    • 若map.get(key)不为空,则将map.get(key)得到的node插入头结点后并返回node.val
  • put(key, value)操作
    • 若map.get(key)为空,则新建node节点,头插法插入双向链表,并加入map:map.put(key, node)。此时若size>capacity,说明超出最大容量,则移除双向链表中的尾结点,同时在map中移除尾结点对应的key。
    • 若map.get(key)不为空,则将map.get(key)得到的node插入头结点后并更新val:node.val = value
  • 数据结构和具体代码实现如下:
    数据结构
import java.util.HashMap;
import java.util.Map;

/**
 * @author: JackieNeo
 * @description: LRU(Least Recently Used)
 * 双向链表 + HashMap实现o(1)的get()和put()时间复杂度
 * @date: 2024/10/21 14:29
 * @version: 1.0
 */
public class LRUCache<K, V> {
    class Node<K, V> {
        K key;
        V val;
        Node pre;
        Node next;

        public Node() {
        }

        public Node(K key, V val, Node pre, Node next) {
            this.key = key;
            this.val = val;
            this.pre = pre;
            this.next = next;
        }
    }

    private Node head;
    private Node tail;
    private Map<K, Node> map;
    private int capacity;
    private int size;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        head = new Node();
        tail = new Node();
        head.next = tail;
        tail.pre = head;
        map = new HashMap<K, Node>();
        size = 0;
    }

    /**
     * 将节点node移动至头结点后
     *
     * @param node
     */
    public void moveToHead(Node node) {
        //断联
        node.pre.next = node.next;
        node.next.pre = node.pre;
        //头插
        node.pre = head;
        node.next = head.next;
        head.next = node;
        node.next.pre = node;
    }

    /**
     * 移除尾结点
     *
     * @param
     * @return
     */
    public Node removeLast() {
        Node last = tail.pre;
        last.pre.next = tail;
        tail.pre = last.pre;
        return last;
    }

    /**
     * 将key对应的Node移动到head后并返回value
     * @param key
     * @return
     */
    public V get(K key) {
        if (map.get(key) == null) return null;
        Node node = map.get(key);
        moveToHead(node);
        return (V) node.val;
    }

    /**
     * key存在:将对应的node移动到head后并更新值为value
     * key不存在:新建节点头插到双向链表,并put到map内
     * 检查size,若超出capacity则在双向链表和map中移除尾结点
     * @param key
     * @param value
     */
    public void put(K key, V value) {
        Node node = map.get(key);
        //key存在,移至头结点并更新值
        if (node != null) {
            moveToHead(node);
            node.val = value;
            return;
        }
        //不存在,新建节点进行头插
        node = new Node(key, value, head, head.next);
        head.next = node;
        node.next.pre = node;
        map.put(key, node);
        size++;
        //超出容量,移除尾部元素
        if (size > capacity) {
            Node last = removeLast();
            map.remove(last.key);
            size--;
        }
    }

    @Override
    public String toString() {
        Node node = head;
        StringBuilder sb = new StringBuilder();
        while(node.next!=tail){
            node = node.next;
            sb.append(node.key + "=" + node.val+" ");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        LRUCache<Integer, String> LRUCache =
                new LRUCache<>(3);
        LRUCache.put(1, "a");
        System.out.println(LRUCache.toString());
        LRUCache.put(2, "b");
        System.out.println(LRUCache.toString());
        LRUCache.put(3, "c");
        System.out.println(LRUCache.toString());
        LRUCache.put(4, "d");
        System.out.println(LRUCache.toString());
        System.out.println(LRUCache.get(3));
        System.out.println(LRUCache.toString());
    }
}
  • 上述代码输出如下:
    代码输入结果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值