LeetCode 724. Find Pivot Index (Java)
class Solution {
public int pivotIndex(int[] nums) {
int sum, leftsum;
sum = 0;
leftsum = 0;
for(int x:nums){
sum+=x;
}
for(int i=0; i<nums.length; i++){
//System.out.println(leftsum+" "+i);
if(leftsum==sum-leftsum-nums[i]){
return i;
}
leftsum += nums[i];
}
//System.out.println(sum);
return -1;
}
}