I submitted my code and its showing wrong answer in your portal while in my machine its showing expected answer?


  • 0
    P

    class LRUCache{
    int capacity;
    int filled;
    list<int> lst;
    map<int,int> mp;
    public:
    LRUCache(int capacity) {
    this->capacity = capacity;
    }

    int get(int key) {
        map<int,int>::iterator it;
    it = mp.find(key);
    list<int>::iterator itlst;
    if(it != mp.end()){
    	itlst = find(lst.begin(), lst.end(), key);
    	if(itlst != lst.end()){
    		lst.erase(itlst);
    		lst.push_back(*itlst);
    	}
    	return mp[key];
    }
    else
    	return -1;
    }
    
    void set(int key, int value) {
    if(capacity){
    	list<int>::iterator itlst;
    	map<int,int>::iterator it;
    	it = mp.find(key);
    	if(it == mp.end()){
    		if(filled == capacity){
    			itlst = lst.begin();
    			mp.erase(*itlst);
    			lst.pop_front();
    			filled--;
    		} 
    		lst.push_back(key);
    		filled++;
    		mp[key] = value;
    	}
    }
    }
    

    };


Log in to reply
 

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