Skip to content

Instantly share code, notes, and snippets.

@Roger7410
Last active October 12, 2016 00:56
Show Gist options
  • Select an option

  • Save Roger7410/d14d2fb4dbdc24293ba42a2b533632a2 to your computer and use it in GitHub Desktop.

Select an option

Save Roger7410/d14d2fb4dbdc24293ba42a2b533632a2 to your computer and use it in GitHub Desktop.
//haha
class Solution{
public List<List<Integer>> threeSum(int[] nums){
List<List<Integer>> results = new ArrayList<>();
if(nums.length < 3) return results;
Arrays.sort(nums);
//i j k are indexs
int i = 0;
while(i < nums.length - 2){
if(nums[i]>0) break;
int j = i + 1;
int k = nums.length - 1;
while(j<k){
int sum = nums[i] + nums[j] + nums[k];
if(sum == 0) results.add(Arrays.asList(nums[i],nums[j],nums[k]));
//try diff j and k with given i
if(sum <= 0) while(nums[j] == nums[++j] && j < k);//deal duplicates
if(sum >= 0) while(nums[k] == nums[--k] && j < k);//deal duplicates
}
while(nums[i] == nums[++i] && i < nums.length -2);//deal duplicates
}
return results;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment