LRU java实现

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

lru相关 https://blog.joway.io/posts/modern-memory-cache/

hashmap中存放key,node;node中存放key,val;hash中存放node是为了快速找到中间的node,node中存放key是为了快速删除hashmap中的key。

class LRUCache {
    int size;
    int capacity;
    private Node head;
    private Node tail;
    private HashMap<Integer,Node> hashnode=new HashMap<>();
    
    public static class Node{
        private Node next;
        private Node prev;
        private int key;
        private int data;
        public Node(int key,int data){
            this.key = key;
            this.data = data;
        }
    
        public int get(){
            return data;
        }
        public void set(int data){
            this.data=data;
        }
        public int getkey(){
            return key;
        }
        private Node(){} 
    }
    public void changehead(Integer key){
        Node node=hashnode.get(key);
        if(head!=node){
            if(tail==node){
                //断尾
                tail=tail.prev;
                //接头
                node.next=head;
                head.prev=node;
                head=node;
            }else{
                //从链中移除
                node.next.prev=node.prev;
                node.prev.next=node.next;
                //接头
                head.prev=node;
                node.next=head;
                head=node;
            }
        }
    }
    public LRUCache(int capacity) {
        this.capacity=capacity;
        size=0;
    }
    public int get(int key) {
        if(hashnode.containsKey(key)){
            //System.out.println(1);
            changehead(key);
            return hashnode.get(key).get();
        }else{
            return -1;
        }
    }

    public void put(int key, int value){
        if(!hashnode.containsKey(key)){
            Node node=new Node(key,value);
            hashnode.put(key,node);
            if(size<capacity){
                if(size==0){
                    head=node;
                    tail=node;
                }else{
                    node.next=head;
                    head.prev=node;
                    head=node;
                }
                size++;
            }else{
                hashnode.remove(tail.getkey());
                //断尾
                tail=tail.prev;
                //接头
                node.next=head;
                head.prev=node;
                head=node;
            }
        //changehead(key);原本是都插入最后一个再changehead
        }else{
            hashnode.get(key).set(value);
            changehead(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);
*/

 

posted @ 2021-01-15 11:05  巴啦啦大魔王  阅读(309)  评论(0编辑  收藏  举报