Why my C solutions runtime error? It is written by C similar to C++


  • 0
    M
    typedef struct {
        int *originalNums;
        int* nums;
        int size;
    } Solution;
    
    Solution* solutionCreate(int* nums, int size) {
        Solution* result = (Solution*)malloc(sizeof(Solution));
        result->nums = (int *)malloc(sizeof(int) * size);
        result->originalNums = (int *)malloc(sizeof(int) * size);
        result->size = size;
        for (int i = 0; i < size; i++){result->nums[i] = nums[i]; result->originalNums[i] = nums[i];}
        //memcpy(result->nums, nums, sizeof(int) * size);
        //memcpy(result->originalNums, nums, sizeof(int) * size);
        return result;
    }
    
    /** Resets the array to its original configuration and return it. */
    int* solutionReset(Solution* obj, int *returnSize) {
        //memcpy(obj->nums, obj->originalNums, sizeof(int) * obj->size);
        for (int i = 0; i < obj->size; i++){obj->nums[i] = obj->originalNums[i];}
        *returnSize = obj->size;
        return obj->nums;
    }
    void swap (int *a, int *b)
    {
        int temp = *a;
        *a = *b;
        *b = temp;
    }
     
    /** Returns a random shuffling of the array. */
    int* solutionShuffle(Solution* obj, int *returnSize) {
        int index;
        int tmp;
        srand(time(NULL));
        for (int i = obj->size-1; i > 0; i--){
            index = rand()%(i+1);
            swap(obj->nums+i, obj->nums+index);
        }
        *returnSize = obj->size;
        return obj->nums;
    }
    
    void solutionFree(Solution* obj) {
        free(obj->originalNums);
        free(obj->nums);
        free(obj);
    }
    
    /**
     * Your Solution struct will be instantiated and called as such:
     * struct Solution* obj = solutionCreate(nums, size);
     * int* param_1 = solutionReset(obj);
     * int* param_2 = solutionShuffle(obj);
     * solutionFree(obj);
     */
    

  • 0

    I have the same problem.


  • 0
    L

    @Mxiaoyu I don't know, I have similar issue. I don't know why. :(


  • 0
    P

    Confirm the issue. C++ code passed, but similar C resulted in run-time error.


  • 0
    M

    Maybe the problem is website.


  • 0

    This was just fixed. Could you please try submitting again?


  • 0

    @1337c0d3r It's OK now.


Log in to reply
 

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