力扣labuladong一刷day52天LRU算法

力扣labuladong一刷day52天LRU算法

概念

LRU的全称为Least Recently Used,翻译出来就是最近最少使用的意思,它是一种内存淘汰算法,当内存不够时,将内存中最久没使用的数据清理掉。 LUR算法是内存管理的一种页面置换算法,就是用来删除内存中不被使用的数据,腾出空间来把常用的数据存进去。

一、146. LRU 缓存

题目链接:https://leetcode.cn/problems/lru-cache/

思路一:使用双向链表加map来手动实现。
class Node {
   
    public int key, val;
    public Node next, prev;
    public Node(int k, int v) {
   
        key = k;
        val = v;
    }
}

class DoubleList{
   
    private Node head, tail;
    private int size;

    public DoubleList() {
   
        head = new Node(0, 0);
        tail = new Node(0, 0);
        head.next = tail;
        tail.prev = head;
        size = 0;
    }

    void addLast(Node x) {
   
        x.next = tail;
        x.prev = tail.prev;
        tail.prev.next = x;
        tail.prev = x;
        size++;
    }

    void remove(Node x) {
   
        x.prev.next = x.next;
        x.next.prev = x.prev;
        size--;
    }

    Node removeFirst() {
   
        if (head.next == tail) {
   
            return null;
        }
        Node first = head.next;
        remove(first);
        return first;
    }
    int size() {
   
        return size;
    }
}

class LRUCache{
   
    private HashMap<Integer, Node> map;
    private DoubleList cache;
    private int cap;

    public int get(int key) {
   
        if (!map.containsKey(key)) {
   
            return -1;
        }
        makeRecently(key);
        return map.get(key).val;
    }

    public void put(int key, int value) {
   
        if (map.containsKey(key)) {
   
            deleteKey(key);
            addRecently(key, value);
            return;
        }
        if (cap == cache.size()) {
   
            deleteLastRecently();
        }
        addRecently(key, value);
    }

    public LRUCache(int capacity) {
   
        this.cap = capacity;
        this.map = new HashMap<>();
        this.cache = new DoubleList();
    }

    private void makeRecently(int key) {
   
        Node node = map.get(key);
        cache.remove(node);
        cache.addLast(node);
    }

    private void addRecently(int key, int value) {
   
        Node node = new Node(key, value);
        map.put(key, node);
        cache.addLast(node);
    }

    private void deleteKey(int key) {
   
        Node node = map.remove(key);
        cache.remove(node);
    }

    private void deleteLastRecently() {
   
        Node node = cache.removeFirst();
        map.remove(node.key);
    }

}

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache obj = new LRUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */
思路二:使用LinkedHashMap

put都是从尾部加入,要想删除头部可以使用map.keySet().iterator().next();拿到key,然后删除。

class LRUCache {
   
    LinkedHashMap<Integer, Integer> map;
    int cap = 0;
    public LRUCache(int capacity) {
   
        map = new LinkedHashMap<>();
        cap = capacity;
    }

    public int get(int key) {
   
        if (!map.containsKey(key)) {
   
            return -1;
        }
        Integer val = map.remove(key);
        map.put(key, val);
        return val;
    }

    public void put(int key, int value) {
   
        if (map.containsKey(key)) {
   
            map.remove(key);
            map.put(key, value);
            return;
        }
        if (cap <= map.size()) {
   
            Integer first = map.keySet().iterator().next();
            map.remove(first);
        }
        map.put(key, value);
    }
}

相关推荐

  1. labuladongday52LRU算法

    2024-01-05 19:02:02       59 阅读
  2. labuladongday53LFU 算法

    2024-01-05 19:02:02       60 阅读
  3. labuladongday59动态规划

    2024-01-05 19:02:02       48 阅读
  4. labuladongday35

    2024-01-05 19:02:02       43 阅读
  5. labuladongday34

    2024-01-05 19:02:02       58 阅读
  6. labuladongday36

    2024-01-05 19:02:02       71 阅读
  7. labuladongday51单调栈应用

    2024-01-05 19:02:02       54 阅读
  8. labuladongday56二叉堆实现优先级队列

    2024-01-05 19:02:02       60 阅读
  9. labuladong——day68

    2024-01-05 19:02:02       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-05 19:02:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-05 19:02:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-05 19:02:02       82 阅读
  4. Python语言-面向对象

    2024-01-05 19:02:02       91 阅读

热门阅读

  1. 计算机网络——网络中要解决的问题

    2024-01-05 19:02:02       62 阅读
  2. 数据光端机与RS-485信号转换技术的实践与应用

    2024-01-05 19:02:02       59 阅读
  3. QuPath学习④ 脚本使用

    2024-01-05 19:02:02       58 阅读
  4. 第四章:智慧变现:探索ChatGPT的赚钱奥秘

    2024-01-05 19:02:02       41 阅读