Compile Error: Line 22: cannot find symbol: method set(int,int)


  • -2
    Y

    I have this compile error indicate in Line 22 "map = new HashMap<Integer,Node>();", cannot find symbol: method set(int,int). I'm confused about this error, is there anyone that can help me out here?

    I've attached my code below:

    public class LRUCache {
    public class Node{
    int val;
    Node pre, next;
    Node(int val){
    this.val=val;
    pre=this;
    next=this;
    }
    }
    private int capacity;
    private Node header;
    private HashMap<Integer,Node> map;
    private int size;

    public LRUCache(int capacity) {
    	this.capacity=capacity;
    	size = 0;
    	header = new Node(-1);
    	header.next=header;
    	header.pre=header;
    	map = new HashMap<Integer,Node>();
    }
    
    public Node isolate(Node n){
    	Node pre = n.pre;
    	Node next = n.next;
    	pre.next=next;
    	next.pre=pre;
    	n.pre=null;
    	n.next=null;
    	size--;
    	return n;
    }
    
    public void addFirst(Node node){
    	if(this.capacity<=size){
    		removeLast();
    		size--;
    	}
    	Node tmp = header.next;
    	header.next=node;
    	node.pre=header;
    	node.next=tmp;
    	tmp.pre=node;
    	size++;
    }
    
    public void removeLast(){
    	Node last = header.pre;
    	Node next = last.pre;
    	header.pre = next;
    	next.next=header;
    	last.next=null;
    	last.pre=null;
    	size--;
    }
    
    public int get(int key){
    	Node n = map.get(key);
    	if(n != null){
    		Node node = isolate(n);
    		addFirst(node);
    		return n.val;
    	}else
    		return -1;
    }
    
    public void put(int key, int value){
    	Node n = new Node(value);
    	if(map.containsKey(key)){
    		Node tmp = map.get(key);
    		isolate(tmp);
    		addFirst(n);
    	}else{
    		addFirst(n);
    	}
    	map.put(key, n);
    }
    

    }


  • 1
    P

    The method set(int, int) is missing.

    You probably should change your put(int, int) back to set(int, int).

    Edit:

    public class LRUCache {
        public class Node {
            int val;
            int key; // Added Key for fast HashMap key removal
            Node pre, next;
            Node(int val, int key) { // Added Key
                this.val=val;
                this.key = key;
                pre=this;
                next=this;
            }
        }
    
        private int capacity;
        private Node header;
        private HashMap<Integer,Node> map;
        private int size;
    
        public LRUCache(int capacity) {
            this.capacity=capacity;
            size = 0;
            header = new Node(-1, -1);
            header.next=header;
            header.pre=header;
            map = new HashMap<Integer,Node>();
        }
    
        public void isolate(Node n){
            Node pre = n.pre;
            Node next = n.next;
            pre.next=next;
            next.pre=pre;
            n.pre=null;
            n.next=null;
            size--;
        }
    
        public void addFirst(Node node){
            if(this.capacity<=size){
                removeLast();
                // size--; 
                // @porker2008: There is no need to do that,
                //  you already do this in removeLast()
            }
            Node tmp = header.next;
            header.next=node;
            node.pre=header;
            node.next=tmp;
            tmp.pre=node;
            size++;
        }
    
        public void removeLast(){
            Node last = header.pre;
            Node next = last.pre;
            header.pre = next;
            next.next=header;
            last.next=null;
            last.pre=null;
            map.remove(last.key); // @porker2008: Remove the key from the HashMap
            size--;
        }
    
        public int get(int key){
            Node n = map.get(key);
            if(n != null){
                isolate(n);
                addFirst(n);
                return n.val;
            }else
                return -1;
        }
    
        public void set(int key, int value){
            Node n = new Node(value, key);
            if(map.containsKey(key)){
                Node tmp = map.get(key);
                isolate(tmp);
            }
            addFirst(n);
            map.put(key, n);
        }
    }

  • 0
    Y

    I directly copy/paste code from my IDE, what a stupid mistake I've made... Thanks bro! Now I have a runtime error: Last executed input:1,[set(2,1),get(2),set(3,2),get(2),get(3)], I've test this input in my IDE which gives me right result. Do you know which part goes wrong here? Thanks!


  • 0
    P

    Please briefly describe your algorithm or add some comments to the code


  • 0

    Please format your code correctly by selecting your code and clicking on the {} button. Also please show the research you've done when you're asking a question, by describing your approach briefly in few sentences so we can help you.


  • 0
    Y

    LRU maintains a hashmap and a doubly linked list with a dummy header.HashMap's key is the key of input set, Map's value points to a list node contains corresponding value of that key. The most recently accessed node(set/put) is always placed next to the dummy header. When size reaches LRU's capacity, always remove dummy header's previous node before add new node. That's the basic idea. Hope it clarifies my question.Thanks.


  • 0
    P

    Answer updated. Please take a look at the comments


  • 0
    Y

    Thank you so much for pointing it out! Appreciate your help.


  • 0
    Y

    Thanks for your attention! @porker2008 has solved my question. I'll follow your suggestion next time when asking questions.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.