用数字元素总和减去当前元素计算剩余元素,和指针total已经经过的元素总和比较
public static int pivotIndex(int[] nums){
int sum = Arrays.stream(nums).sum();
int total = 0;
for (int i = 0; i < nums.length; i++) {
total=nums[i]+total;
if (total==sum){
return i;
}
sum=sum-nums[i];
}
return -1;
}