Accepted C solution


  • 5
    Y
    struct NumArray {
        int size;
        int *sum;
    

    };

    /** Initialize your data structure here. */

    struct NumArray* NumArrayCreate(int* nums, int numsSize) {
        struct NumArray *array;
        int i;
    
        array = (struct NumArray*)calloc(1, sizeof(struct NumArray));
        array->sum = (int *) calloc(numsSize+1, sizeof(int));
        array->size = numsSize;
        array->sum[0] = 0;
        for(i = 0; i < numsSize; i++){
                array->sum[i+1] = nums[i] + array->sum[i];
        }
        return array;
    

    }

     int sumRange(struct NumArray* numArray, int i, int j)
    {
        int sum = 0;
        if(!numArray)
                return 0;
        sum = numArray->sum[j+1] - numArray->sum[i];
        return sum;
    

    }

    /** Deallocates memory previously allocated for the data structure. */
    void NumArrayFree(struct NumArray* numArray) {
        free(numArray->sum);
        free(numArray);
    

    }


Log in to reply
 

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