class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int len = nums.length;
Arrays.sort(nums);
List<List<Integer>> list = new ArrayList<>();
for(int first = 0; first < len; first++){
if(first > 0 && nums[first] == nums[first - 1]){
continue;
}
int third = len - 1;
int target = -nums[first];
for(int second = first + 1; second < len; second++){
if(second > first + 1 && nums[second] == nums[second - 1]){
continue;
}
while(second < third && nums[second] + nums[third] > target){
third--;
}
if(second == third){
break;
}
if(nums[second] + nums[third] == target){
List<Integer> l = new ArrayList<>();
l.add(nums[first]);
l.add(nums[second]);
l.add(nums[third]);
list.add(l);
}
}
}
return list;
}
}
剑指-007
最新推荐文章于 2024-07-25 11:27:31 发布