class Solution {
public static List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> list = new ArrayList<>();
int flag = 1;
for (int i = 0;i<nums.length;i++){
if ( i> 0 && nums[i] == nums[i - 1]) {
continue;
}
int j = i+1;
int k = nums.length-1;
while (j < k){
if (j>i+1 && nums[j] == nums[j-1]){
j++;
continue;
}
if(nums[j]+nums[k]+nums[i] > 0){
k--;
}else if(nums[j]+nums[k]+nums[i] < 0){
j++;
}else{
list.add(Arrays.asList(nums[i],nums[j],nums[k]));
j++;
}
}
}
return list;
}
}
剑指 Offer II 007. 数组中和为 0 的三个数(Java)
最新推荐文章于 2024-08-22 17:27:46 发布