class Solution {
public int pivotIndex(int[] nums) {
int leftsum = 0, sum = 0;
//get the sum first, then check if the leftsum is equal to the rest of the array
for(int i = 0; i < nums.length ; i++){
sum += nums[i];
}
for(int i = 0; i < nums.length; i++){
if(leftsum == sum - nums[i] - leftsum) return i; // which is the rightside sum
leftsum += nums[i];
}
return -1;
}
}
724. Find Pivot Index
最新推荐文章于 2021-04-18 21:14:39 发布