Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public List<List<Integer>> threeSum(int[] nums) {
- List<List<Integer>> result = new ArrayList<List<Integer>>();
- Arrays.sort(nums);
- int i = 0;
- while (i < nums.length) {
- int l = i+1;
- int r = nums.length - 1;
- if (i>0 && nums[i] == nums[i-1]) {
- i++;
- continue;
- }
- while (l < r) {
- int sum = nums[i] + nums[l] + nums[r];
- if (sum == 0) {
- List<Integer> tmpList = new ArrayList<Integer>();
- tmpList.add(nums[i]);
- tmpList.add(nums[l]);
- tmpList.add(nums[r]);
- result.add(tmpList);
- while (l < r && nums[l] == nums[l+1]) l += 1;
- while (l < r && nums[r] == nums[r-1]) r -= 1;
- l += 1;
- r -= 1;
- }
- else if (sum < 0) l += 1;
- else r -= 1;
- }
- i++;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement