Test case wrong


  • 2
    D

    Test case error:

    Submission Result: Wrong Answer
    Input: 1,[set(2,1),get(2),set(3,2),get(2),get(3)]
    Output: [1,1,2]
    Expected: [1,-1,2]

    {
    struct Node
    {
    int value;
    int visit;
    };

    typedef std::list <Node*> NODELIST;

    class LRUCache{
    public:
    LRUCache(int capacity) {
    m_capacity=capacity;
    count=0;
    nmap.clear();
    }

    int get(int key) {
        int val=-1;
        std::map<int, NODELIST::iterator>::iterator it;
        it=nmap.find(key);
        if (it!=nmap.end())
        {
            NODELIST::iterator i=it->second;
            val=(*i)->value;
            (*i)->visit++;
        }
    
        return val;
    }
    
    void set(int key, int value) {
        Node* node=new Node;
        node->value=value;
        node->visit=0;
        nlist.push_front(node);
        nmap.insert(std::pair<int, NODELIST::iterator> (key,nlist.begin()));
    }
    

    protected:
    int m_capacity;
    int count;
    NODELIST nlist;
    std::map<int, NODELIST::iterator> nmap;

    };
    }


  • 0

    Can you explain what's wrong in the test case?


  • 0
    D

    The size of LRU cache is 1. So after set(3, 2), there is only one element in the cache.


Log in to reply
 

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