Got Runtime Error but the code works fine on my machine

  • 0

    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{
        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;
                        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;
                return -1;
        void set(int key, int value) {
            if (this->myMap.size() == capacity) {
                DListNode *delNode = this->tail;
                if (delNode->prev){
                    delNode->prev->next = NULL;
                    this->tail = delNode->prev;
                    this->head = this->tail = NULL;
            DListNode *popNode;
            if (!this->myMap.count(key)) {
                popNode = new DListNode(key, value);
                this->myMap[key] = popNode;
                DListNode *popNode = this->myMap[key];
                popNode->val = value;
            if (this->head) {
                this->head->prev = popNode;
                popNode->next = this->head;
                this->head = popNode;
                this->head = this->tail = popNode;
        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

    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

    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

    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

    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.