Last active
October 12, 2016 00:56
-
-
Save Roger7410/d14d2fb4dbdc24293ba42a2b533632a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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