其他分享
首页 > 其他分享> > LeetCode146-LRU缓存机制

LeetCode146-LRU缓存机制

作者:互联网

题目:

运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制 。

实现 LRUCache 类:
LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存
int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
void put(int key, int value): 如果关键字已经存在,则变更其数据值;
如果关键字不存在,则插入该组「关键字-值」。
当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。

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

解法1:

class LRUCache {

    private int cacheSize;
    private LinkedHashMap<Integer,Integer> map;

    public LRUCache(int capacity) {
        this.cacheSize = capacity;
        map = new LinkedHashMap<>(cacheSize,0.75F,true){
    
        @Override 
        protected boolean removeEldestEntry(Map.Entry<Integer,Integer> eldest){
            return size() > cacheSize;
        }
    };
   }
    
    public int get(int key) {
        return map.getOrDefault(key,-1);
    }
    
    public void put(int key, int value) {
        map.put(key,value);
    }
}

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

解法2

class LRUCache {

    private HashMap<Integer, Node> map;
    private int cacheSize;
    private LinkedNodeList cache;

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

    public int get(int key) {
        if (!map.containsKey(key))
            return -1;
        Node res = map.get(key);
        cache.remove(res);
        cache.addFirst(res);
        return res.value;
    }

    public void put(int key, int value) {
        Node newnode = new Node(key, value);
        if (map.containsKey(key)) {
            cache.remove(map.get(key));
            cache.addFirst(newnode);
            map.put(key, newnode);
        } else {
            if (cacheSize == map.size()) {
                Node lastnode = cache.removeLast();
                map.remove(lastnode.key);
            }
            cache.addFirst(newnode);
            map.put(key, newnode);
        }
    }
}

class Node {
    int key;
    int value;
    Node prev;
    Node next;
    public Node() {
    }
    public Node(int k, int v) {
        this.key = k;
        this.value = v;
    }
}
class LinkedNodeList {
    Node head;
    Node tail;
    public LinkedNodeList() {
        head = new Node(0, 0);
        tail = new Node(0, 0);
        head.next = tail;
        tail.prev = head;
    }
    public void addFirst(Node n) {
        head.next.prev = n;
        n.next = head.next;
        n.prev = head;
        head.next = n;
    }
    public void remove(Node n) {
        n.prev.next = n.next;
        n.next.prev = n.prev;
    }
    public Node removeLast() {
        Node res = tail.prev;
        remove(res);
        return res;
    }
}



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

标签:Node,map,缓存,LeetCode146,int,LRU,key,public,LRUCache
来源: https://www.cnblogs.com/alwayszzj/p/15420274.html