lc15 三数之和
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if(i>0 && nums[i] == nums[i-1]) {
continue;
}
int k = nums.length-1;
int t = -nums[i];
for (int j = i + 1; j < nums.length; j++) {
if(j != i + 1 && nums[j] == nums[j-1]){
continue;
}
while(j < k && nums[j] + nums[k] > t) {
k--;
}
if (j == k) {
break;
}
if (nums[j] + nums[k] == t) {
ans.add(Arrays.asList(nums[i],nums[j],nums[k]));
}
}
}
return ans;
}
}
注意处理相同数字的逻辑
标签:lc,nums,int,0926,top,length,&&,ans From: https://www.cnblogs.com/beichuang/p/16732806.html