力扣labuladong——一刷day88

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣146. LRU 缓存(手动实现LinkedHashMap)
  • 二、力扣力扣146. LRU 缓存


前言


LRU 缓存淘汰算法就是一种常用策略。LRU 的全称是 Least Recently Used,也就是说我们认为最近使用过的数据应该是是「有用的」,很久都没用过的数据应该是无用的,内存满了就优先删那些很久没用过的数据。

一、力扣146. LRU 缓存(手动实现LinkedHashMap)

class LRUCache {
    private Map<Integer, Node> map;
    private DoubleList doubleList;
    private int capacity;

    public LRUCache(int capacity) {
        this.map = new HashMap<>();
        this.doubleList = new DoubleList();
        this.capacity = capacity;
    }
    
    public int get(int key) {
        if(!map.containsKey(key)){
            return -1;
        }
        return makeRecently(key);
    }
    
    public void put(int key, int value) {
        if(map.containsKey(key)){
            deleteKey(key);
            addRecently(key,value);
            return;
        }
        if(capacity == doubleList.getSize()){
            clearRecently();
        }
        addRecently(key,value);
    }
    private int makeRecently(int key){
        Node x = map.get(key);
        doubleList.remove(x);
        doubleList.add(x);
        return x.val;
    }
    private void addRecently(int key, int val){
        Node x = new Node(key,val);
        map.put(key,x);
        doubleList.add(x);
    }
    private void deleteKey(int key){
        Node x = map.remove(key);
        doubleList.remove(x);
    }
    private void clearRecently(){
        Node x = doubleList.removeLast();
        map.remove(x.key);
    }
    class DoubleList{
        public Node head, tail;
        public int size;
        public DoubleList(){
            this.head = new Node(0,0);
            this.tail = new Node(0,0);
            this.head.next = this.tail;
            this.tail.prev = this.head;
            this.size = 0;
        }
        public void remove(Node node){
            node.prev.next = node.next;
            node.next.prev = node.prev;
            this.size --;
        }
        public Node removeLast(){
            if(this.head.next == this.tail){
                return null;
            }
            Node res = this.head.next;
            remove(res);
            return res;
        }
        public void add(Node node){
            node.next = this.tail;
            node.prev = this.tail.prev;
            this.tail.prev.next = node;
            this.tail.prev = node;
            this.size ++;
        }
        public int getSize(){
            return this.size;
        }
    }
    class Node{
        public int key;
        public int val;
        public Node prev, next;
        public Node(int key, int val){
            this.key = key;
            this.val = val;
        }
    }
}

/**
 * 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);
 */

二、力扣力扣146. LRU 缓存

class LRUCache {
    private int cap;
    private LinkedHashMap<Integer, Integer> cache;
    public LRUCache(int capacity) {
        this.cap = capacity;
        this.cache = new LinkedHashMap<>();
    }
    
    public int get(int key) {
        if(!cache.containsKey(key)){
            return -1;
        }
        makeRecnetly(key);
        return cache.get(key);
    }
    
    public void put(int key, int value) {
        if(cache.containsKey(key)){
            cache.put(key,value);
            makeRecnetly(key);
            return;
        }
        if(cache.size() >= this.cap){
            int k = cache.keySet().iterator().next();
            cache.remove(k);
        }
        cache.put(key,value);
    }
    public void makeRecnetly(int key){
        int val = cache.get(key);
        cache.remove(key);
        cache.put(key, val);
    }
}

/**
 * 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);
 */

你可能感兴趣的:(力扣题解,leetcode,java,spring,算法,数据结构)