前缀和
class Solution {
public int pivotIndex(int[] nums) {
int a[] = new int[nums.length+1];
int b[] = new int[nums.length+1];
for(int i=1; i<= nums.length; i++) {
a[i] = a[i-1] + nums[i-1];
}
for(int i=nums.length-1; i>=0; i--) {
b[i] = b[i+1] + nums[i];
}
for(int i=0; i< nums.length; i++) {
if(a[i] == b[i+1]) {
return i;
}
}
return -1;
}
}
网友评论