class Solution {
public int pivotIndex(int[] nums) {
if(nums.length == 0) return -1;
long sum = 0;
for(int i = 0; i < nums.length; i++)
sum += nums[i];
int index = 0;
long leftSum = 0;
long rightSum;
for(; index < nums.length; index++){
if(index != 0)
leftSum += nums[index - 1];
rightSum = sum - leftSum - nums[index];
if(leftSum == rightSum)
break;
}
if(index != nums.length)
return index;
else
return -1;
}
}
来源于力扣:寻找数组的中心索引