Why my C code is "runtime error"? Thanks


  • -1
    I
    struct NumArray {
        int sum;
    };
    

    /** Initialize your data structure here. */

        struct NumArray* NumArrayCreate(int* nums, int numsSize) {
            	struct NumArray *neww;
        	neww=(struct NumArray *)malloc(numsSize * sizeof(struct NumArray));
        	neww[0].sum=0;
        	for (int i=1;i<=numsSize;i++)
        	{
        		neww[i].sum=neww[i-1].sum+nums[i-1];
        	}
        	return neww;
        }
    
    int sumRange(struct NumArray* numArray, int i, int j) {
         return (numArray[j+1].sum-numArray[i].sum);
    }
    

    /** Deallocates memory previously allocated for the data structure. */

    void NumArrayFree(struct NumArray* numArray) {
        free(numArray);
    }
    

    // Your NumArray object will be instantiated and called as such:
    // struct NumArray* numArray = NumArrayCreate(nums, numsSize);
    // sumRange(numArray, 0, 1);
    // sumRange(numArray, 1, 2);
    // NumArrayFree(numArray);


Log in to reply
 

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