724_寻找数组的中心下标
小于 1 分钟
724_寻找数组的中心下标简单
class Solution {
public int pivotIndex(int[] nums) {
int len = nums.length;
int sum = 0;
for(int num : nums) sum += num;
int mid = 0;
int left = 0;
for(;mid<len;mid++){
left+=nums[mid];
if(left == sum - left + nums[mid]){
return mid;
}
}
return -1;
}
}
计算总和O(n)
比较左边和与总和减去左边和O(n)
Powered by Waline v2.15.5