A simple C solution [Accepted]


  • 0
    B
    int pivotIndex(int* nums, int numsSize) {
        if(numsSize==0)
        {return -1;}
        int left_sum=0,right_sum=0;
        for(int i=1;i<numsSize;i++)
        {
            right_sum=right_sum+nums[i];
        }
        if(left_sum==right_sum)
        {return 0;}
        for(int i=1;i<numsSize;i++)
        {
            left_sum=left_sum+nums[i-1];
            right_sum=right_sum-nums[i];
            if(left_sum==right_sum)
            {return i;}
        }
        return -1;
    }
    
    

Log in to reply
 

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