Result mismatch


  • 0
    G
    struct triple{
        struct triple* older;
        struct triple* younger;
        int val;
        int key;
        
        triple(int k, int v){
            key = k;
            val = v;
            older = nullptr;
            younger = nullptr;
        };
    };
    
    map<int, struct triple*> dataArray;
    
    struct triple* youngest;
    struct triple* oldest;
    
    int cap;
    
    LRUCache(int capacity) {
        youngest = nullptr;
        oldest = nullptr;
        
        cap = capacity;
    }
    
    int get(int key) {
        if(dataArray.find(key) == dataArray.end())
            return -1;
            
        struct triple *r = dataArray.find(key)->second;
        
        if(r != youngest)
        {
            if(r == oldest)
            {
                oldest = r->younger;
                oldest->older = nullptr;
                
                r->older = youngest;
                youngest->younger = r;
                
                youngest = r;
                r->younger = nullptr;
            }
            else
            {
                r->younger->older = r->older;
                r->older = youngest;
                youngest->younger = r;
                youngest = r;
            }
        }
        
        return dataArray[key]->val;
    }
    
    void set(int key, int value) {
    
        struct triple* r = nullptr;
        if(dataArray.find(key) != dataArray.end())
        {
            r = dataArray.find(key)->second;
            r->val = value;
        }
        else
        {
            r = new struct triple(key, value);
            dataArray[key] = r;
        }
        
        if(youngest == nullptr)
        {
            youngest = r;
            r->younger = nullptr;
            r->older = nullptr;
            
            oldest = r;
        }
        else
        {
            youngest->younger = r;
            r->older = youngest;
            r->younger = nullptr;
            youngest = r;
        }
        
        if(dataArray.size() > cap)
        {
            dataArray.erase(oldest->key);
            
            oldest = oldest->younger;
    
            oldest->older = nullptr;
        }
    }
    

    My code generate correct results on my local machine (g++ 4.6.3), however, it gives incorrect results on Leetcode when tested on following test case:

    Input: 2,[set(2,1),set(1,1),set(2,3),set(4,1),get(1),get(2)]
    Output: [1,-1]
    Expected: [-1,3]

    I understand that leetcode is using g++ 4.7.9, but I don't think this looks like a version difference issue. Could someone help me with this?


Log in to reply
 

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