Got Runtime Error but the code works fine on my machine


  • 0
    W

    Does anyone has the same problem? I've also tried g++ -m32 to compile the code, it print correct result on my machine.

    P.S Last executed input: 2,[set(2,1),set(2,2),get(2),set(1,1),set(4,1),get(2)], so the problem shouldn't be input is too large.

    here's the code:

    #include <iostream>
    #include <map>
    
    using namespace std;
    
    class LRUCache{
    public:
        LRUCache(int capacity) {
            this->capacity = capacity;
            this->head = NULL;
            this->tail = NULL;
        }
        
        int get(int key) {
            if (this->myMap.count(key)) {
                DListNode *target = this->myMap[key];
                if (target != head){
                    if (target->next) {
                        target->prev->next = target->next;
                        target->next->prev = target->prev;
                    }else{
                        target->prev->next = NULL;
                        this->tail = target->prev;
                    }
                    
                    this->head->prev = target;
                    target->next = this->head;
                    target->prev = NULL;
                    this->head = target;
                }
                
                return target->val;
            }else{
                return -1;
            }
        }
        
        void set(int key, int value) {
            if (this->myMap.size() == capacity) {
                DListNode *delNode = this->tail;
                this->myMap.erase(delNode->key);
                if (delNode->prev){
                    delNode->prev->next = NULL;
                    this->tail = delNode->prev;
                }else{
                    this->head = this->tail = NULL;
                }
                
                delete(delNode);
            }
            
            DListNode *popNode;
            if (!this->myMap.count(key)) {
                popNode = new DListNode(key, value);
                this->myMap[key] = popNode;
            }else{
                DListNode *popNode = this->myMap[key];
                popNode->val = value;
            }
            
            if (this->head) {
                this->head->prev = popNode;
                popNode->next = this->head;
                this->head = popNode;
            }else{
                this->head = this->tail = popNode;
            }
        }
    private:
        struct DListNode {
            int val;
            int key;
            DListNode *next;
            DListNode *prev;
            DListNode(int key, int value) : key(key), val(value), next(NULL), prev(NULL) {}
        };
        
        int capacity;
        map<int, DListNode*> myMap;
        DListNode *head;
        DListNode *tail;
    };
    
    
    int main(){
        LRUCache *cache = new LRUCache(2);
        // 2,[set(2,1),set(2,2),get(2),set(1,1),set(4,1),get(2)]
        cache->set(2, 1);
        cache->set(2, 2);
        
        cout << cache->get(2) << endl;
        
        cache->set(1, 1);
        cache->set(4, 1);
        
        cout << cache->get(2) << endl;
        
        return 0;
    }

  • -3
    P

    It is very hard to judge what type of runtime error you have.

    It depends on your implementation. You can edit your question and add your code. We can then figure it out.


  • 0
    N

    the same problem occurs to me , I wonder may be we all use the link method, and it not allowed define linked list by ourself ? now I haven't find the reason.


  • 0
    X

    I have the exaxct same situation now. First I had an accepted version wich used my own implemented Doubly linked list. Then, I had decided to switch to STL list, which caused an unexpected runtime error here and works fine on my machine.


  • 0
    N

    I tried STL list , it is double linked list and use its splice function, it works fine.
    may the swap algrithm time complication is better than the code write by our self.


Log in to reply
 

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