寻找数组的中心索引
class Solution {
public int pivotIndex(int[] nums) {
int n = nums.length,sum = 0;
for(int i = 0;i<n;i++){
sum+=nums[i];
}
int sum_l = 0;
for(int i =0;i<n;i++){
sum -= nums[i];
if(sum_l == sum){
return i;
}
sum_l += nums[i];
}
return -1;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17