class Solution {
public int pivotIndex(int[] nums) {
int total = Arrays.stream(nums).sum();
int sum=0;
for (int i = 0; i < nums.length; i++) {
if(sum==total-sum-nums[i]){
return i;
}
sum+=nums[i];
}
return -1;
}
}
前缀和
sum起始:0 //中心数位于数组最左端,左侧和视为0
nums[i]----左边:sum;右边:total-sum-nums[i]