leetcode [146]LRU Cache

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

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(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.node

The cache is initialized with a positive capacity.this

Follow up:
Could you do both operations in O(1) time complexity?spa

Example:code

LRUCache cache = new LRUCache( 2 /* capacity */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // returns 1
cache.put(3, 3);    // evicts key 2
cache.get(2);       // returns -1 (not found)
cache.put(4, 4);    // evicts key 1
cache.get(1);       // returns -1 (not found)
cache.get(3);       // returns 3
cache.get(4);       // returns 4

題目大意:blog

實現一個LRU結構。ip

解法:ci

採用Hashmap+雙向鏈表進行實現,雙向鏈表記錄訪問次序,HashMap幫助實現時間複雜度爲O(1)的查找效率。rem

java:get

class LRUCache {
    private int capacity;
    private int count;
    private DLinkedNode head;
    private DLinkedNode tail;
    private Map<Integer,DLinkedNode>cache;

    class DLinkedNode{
        int key;
        int value;
        DLinkedNode pre;
        DLinkedNode next;

        public  DLinkedNode(){ }

        public DLinkedNode(int key,int value){
            this.key=key;
            this.value=value;
            this.pre=null;
            this.next=null;
        }
    }

    public LRUCache(int capacity) {
        this.count=0;
        this.capacity=capacity;
        this.cache=new HashMap<>();

        head=new DLinkedNode();
        tail=new DLinkedNode();

        head.pre=null;
        tail.next=null;

        head.next=tail;
        tail.pre=head;
    }

    private void addTohead(DLinkedNode node){
        node.next=head.next;
        head.next.pre=node;

        node.pre=head;
        head.next=node;
    }

    private void removeNode(DLinkedNode node){
        node.next.pre=node.pre;
        node.pre.next=node.next;

        node.pre=null;
        node.next=null;
    }

    private void moveToHead(DLinkedNode node){
        removeNode(node);
        addTohead(node);
    }

    private void popTail(){
        DLinkedNode tmp=tail.pre.pre;
        tmp.next=tail;
        tail.pre=tmp;
    }

    public int get(int key) {
        DLinkedNode tmp=cache.get(key);
        if (tmp!=null){
            moveToHead(tmp);
            return tmp.value;
        }

        return -1;
    }

    public void put(int key, int value) {
        if(!cache.containsKey(key)){
            DLinkedNode node=new DLinkedNode(key,value);
            addTohead(node);
            cache.put(key,node);
            count++;

            if (count>capacity){
                cache.remove(tail.pre.key);
                popTail();
                count--;
            }
        }else{
            DLinkedNode tmp=cache.get(key);
            tmp.value=value;
            moveToHead(tmp);
        }
    }
}
相關文章
相關標籤/搜索