In which order Unorderd_map stored values. This solution shows answer wrong.


  • 0
    R
    class LRUCache{
    public:
        int limit;
        unordered_map<int,int> keyval;
        LRUCache(int capacity) {
            limit=capacity;
        }
        int get(int key) {
            if(keyval.find(key)!=keyval.end()){
                return keyval.find(key)->second;
            }else{
                return -1;
            }
         }
        
        void set(int key, int value) {
            if(keyval.find(key)!=keyval.end()){
                keyval.erase(keyval.find(key));
                keyval.insert(pair<int,int>(key,value));
            }else if(keyval.size()<limit){
                keyval.insert(pair<int,int>(key,value));
            }else{
                keyval.erase(keyval.begin());
                keyval.insert(pair<int,int>(key,value));
            }
        }
    };`enter code here`

Log in to reply
 

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