Why do I get a wrong answer?


  • 0
    5

    I wrote the code in my own IDE and copied to leetcode, it gave me a wrong answer but it worked well in my own machine using the given input.

        int size = 0;
    list<int> q;
    int c;
    map<int,int > m;
    
    class LRUCache{
    public:
        LRUCache(int capacity);
    
        int get(int key);
    
        void set(int key, int value);
    };
    
    
    
    LRUCache::LRUCache(int capacity) {
    	c = capacity;
        }
    
    int LRUCache::get(int key) {
        auto it = m.find(key);
    	if(it != m.end()){
            q.remove(it->first);
    		q.push_back(it->first);
    			return it->second;
    	}else{
    		return -1;
    	}
    
       }
    void LRUCache::set(int key, int value) {
        	if(size == c){
        		int front = q.front();
        		q.pop_front();
        		m.erase(front);
        	}else{
        		size++;
        	}
        	q.push_back(key);
        	m[key] = value;
        }

Log in to reply
 

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