【leetcode】寻找数组的中心索引

 

int pivotIndex(int* nums, int numsSize){
    int i,total=0,sum=0;
    for (i=0; i<numsSize; i++)
        total+=nums[i];
    for (i=0; i<numsSize; i++){
        if (sum*2 + nums[i] == total)
            return i;
        sum    += nums[i];
    }
    return -1;
}

 

posted @ 2020-09-29 13:46  温暖了寂寞  阅读(90)  评论(0编辑  收藏  举报