题目地址
https://leetcode.cn/problems/find-pivot-index/description/
题目描述
代码
class Solution {
public int pivotIndex(int[] nums) {
int total = Arrays.stream(nums).sum();
//前缀和
int prefixSum =0;
int len = nums.length;
for(int i =0;i<len;i++){
if (i-1>=0){
prefixSum+=nums[i-1];
}
//当前元素的后缀和 = total-nums[i]-prefixSum
if((total-nums[i]-prefixSum)==prefixSum){
return i;
}
}
return -1;
}
}