I am really confused why this solution didn't work


  • 0
    L

    I push the new element into the queue but the size of queue is still zero after pushing.
    Is there something wrong with the compiler of leetcode? Somebody help me plz!

    LRUCache(int capacity) {
    cap = capacity;
    size = 0;
    }

    int get(int key) {
        if (!myMap.count(key))
            return -1;
        else return myMap[key];
    }
    
    void set(int key, int value) {
    
        if (size+1 >= cap) {
            cout << keys.front() << endl;
            cout << keys.size() << endl;
            myMap.erase(keys.front());
            keys.pop();
            if (size > 0)
                size--;
            else size = 0;
        }
        if (!myMap.count(key)) {
            cout << "key: " << key << endl;
            keys.push(key);
            cout << "size: " << keys.size() << endl;
            size++;
        }
        myMap[key] = value;
    }
    private:
        unordered_map<int, int> myMap;
        queue <int> keys;
        int size;
        int cap;

Log in to reply
 

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