【LeetCode】【HOT】15. 三数之和
文章目录
package hot;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution15 {
public static void main(String[] args) {
int[] nums = {-1,0,1,2,-1,-4};
Solution15 solution = new Solution15();
System.out.println(solution.method(nums));
}
private List<List<Integer>> method(int[] nums){
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for(int k = 0; k < nums.length - 2; k++){
if(nums[k] > 0) break;
if(k > 0 && nums[k] == nums[k - 1]) continue;
int i = k + 1;
int j = nums.length - 1;
while(i < j){
int sum = nums[k] + nums[i] + nums[j];
if(sum < 0){
while(i < j && nums[i] == nums[++i]);
}else if(sum > 0){
while(i < j && nums[j] == nums[--j]);
}else{
res.add(Arrays.asList(nums[k], nums[i], nums[j]));
while(i < j && nums[i] == nums[++i]);
while(i < j && nums[j] == nums[--j]);
}
}
}
return res;
}
}
//时间复杂度为 O(n^2)
//空间复杂度为 O(1)