public class ReSum {
public static int pivotIndex(int[] nums) {
int[] sum = new int[nums.length];
sum[0] = nums[0];
for(int i=1;i<nums.length;i++){
sum[i] = sum[i-1] + nums[i];
}
for(int i=0;i<nums.length;i++){
if(i==0){
if(sum[nums.length-1] - sum[0] ==0){
return 0;
}
else{
continue;
}
}
if(sum[i-1] == sum[nums.length-1]-sum[i]){
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] nums = {1, 7, 3, 6, 5, 6};
System.out.println(pivotIndex(nums));
}
}
12-08
451