146. LRU Cache - jiejackyzhang/leetcode-note GitHub Wiki

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

解题思路为hash map + double linked list。

用hash map可以在constant time内完成contains和get操作,用来判断元素是否存在和获取元素。 用double linked list的好处是删除一个node不需要额外的reference,而且从head何tail处删/添操作都是constant time。 可以用head和tail指针指向list的头节点和尾节点,认为尾节点的元素是最近被使用的,因此一个元素只要被get(key)或set(key)操作过,则移至尾端。若cache已满,则从头节点处删除,新的节点添加至尾端。

public class LRUCache {
    
    private Map<Integer, DLinkNode> cache;
    DLinkNode tail = null;
    DLinkNode head = null;
    int capacity;
    
    public LRUCache(int capacity) {
        cache = new HashMap<>();
        this.capacity = capacity;
    }
    
    public int get(int key) {
        if(cache.containsKey(key)) {
            DLinkNode target = cache.get(key);
            int value = target.value;
            target.update();
            return value;
        } else {
            return -1;
        }
    }
    
    public void set(int key, int value) {
        if(cache.containsKey(key)) {
            DLinkNode target = cache.get(key);
            target.value = value;
            target.update();
        } else {
            if(capacity == 0) return;
            if(cache.size() == capacity) {
                cache.remove(head.key);
                head.removeFromHead();
            }
            DLinkNode newNode = new DLinkNode(key, value);
            newNode.append();
            cache.put(key, newNode);
        }
    }
    
    class DLinkNode {
        int key;
        int value;
        DLinkNode prev = null;
        DLinkNode next = null;
        
        public DLinkNode(int key, int value) {
            this.key = key;
            this.value = value;
        }
        
        // remove head
        public void removeFromHead() {
            // if 'this' is the only node, set both tail and head to null
            if(tail == this) {
                head = null;
                tail = null;
            } else {
                head = this.next;
                head.prev = null;
            }
        }
        
        // move node to tail
        public void update() {
            if(tail == this) return;
            // remove form current position
            if(this != head) {
                this.prev.next = this.next;
            } else {
                head = this.next;
            }
            this.next.prev = this.prev;
            // append to tail
            this.append();
        }
        
        // append node to tail
        public void append() {
            if(tail == null) {
                // the first node
                head = this;
                tail = this;
            } else {
                this.next = null;
                this.prev = tail;
                tail.next = this;
                tail = this;
            }
        }
    }
}