|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: easy |
| 4 | +# Follow `Topics` tags |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Two Pointers |
| 8 | + - Sorting |
| 9 | +--- |
| 10 | + |
| 11 | +# [15. 3Sum](https://leetcode.com/problems/3sum/description/) |
| 12 | + |
| 13 | +## Description |
| 14 | +You're given an array of integers, `nums`. Your task is to find all unique sets of three numbers \([nums[i], nums[j], nums[k]]\) such that: |
| 15 | + |
| 16 | +1. The indices \(i\), \(j\), and \(k\) are distinct (\(i \neq j \neq k\)). |
| 17 | +2. The sum of the three numbers is zero: \(nums[i] + nums[j] + nums[k] = 0\). |
| 18 | +3. No duplicate triplets should appear in the result set. |
| 19 | + |
| 20 | +Return a list of all such triplets. |
| 21 | + |
| 22 | + |
| 23 | +**Example 1:** |
| 24 | +``` |
| 25 | +Input: nums = [[-2, -1, 0, 1, 3]] |
| 26 | +Output: [[-1, 0, 1], [-2, -1, 3]] |
| 27 | +``` |
| 28 | + |
| 29 | +**Example 2:** |
| 30 | +``` |
| 31 | +Input: nums = [[-2, -1, 1, -1, 2, 1]] |
| 32 | +Output: [[-1, -1, 2], [-2, 1, 1]] |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | +**Constraints:** |
| 37 | +`3 <= nums.length <= 3000` |
| 38 | +`-10^5 <= nums[i] <= 10^5` |
| 39 | + |
| 40 | +## Solution |
| 41 | + |
| 42 | +Fix a value and use two pointers from bengin and end to loop all combinations sum equal 0. |
| 43 | + |
| 44 | +```java |
| 45 | +class Solution { |
| 46 | + public List<List<Integer>> threeSum(int[] nums) { |
| 47 | + List<List<Integer>> res = new ArrayList<>(); |
| 48 | + Arrays.sort(nums); |
| 49 | + for (int k = 0; k < nums.length - 2; k++) { |
| 50 | + if (k > 0 && nums[k] == nums[k - 1]) |
| 51 | + continue; |
| 52 | + int i = k + 1, j = nums.length - 1; |
| 53 | + while (i < j) { |
| 54 | + int sum = nums[k] + nums[i] + nums[j]; |
| 55 | + if (sum < 0) { |
| 56 | + i++; |
| 57 | + } else if (sum > 0) { |
| 58 | + j--; |
| 59 | + } else { |
| 60 | + res.add(Arrays.asList(nums[k], nums[i], nums[j])); |
| 61 | + while (i < j && nums[i] == nums[i + 1]) |
| 62 | + i++; |
| 63 | + while (i < j && nums[j] == nums[j - 1]) |
| 64 | + j--; |
| 65 | + i++; |
| 66 | + j--; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + return res; |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +```python |
| 76 | +class Solution: |
| 77 | + def threeSum(self, nums: list[int]) -> list[list[int]]: |
| 78 | + nums.sort() |
| 79 | + n = len(nums) |
| 80 | + returnList = [] |
| 81 | + for i, i_num in enumerate(nums): |
| 82 | + if i and i_num == nums[i-1]: |
| 83 | + continue |
| 84 | + l = i + 1 |
| 85 | + r = n - 1 |
| 86 | + while l < r: |
| 87 | + sum = i_num + nums[l] + nums[r] |
| 88 | + if sum == 0: |
| 89 | + returnList.append([i_num, nums[l], nums[r]]) |
| 90 | + while l < r and nums[l] == nums[l+1]: |
| 91 | + l += 1 |
| 92 | + while l < r and nums[r] == nums[r-1]: |
| 93 | + r -= 1 |
| 94 | + l += 1 |
| 95 | + r -= 1 |
| 96 | + elif sum < 0: |
| 97 | + l += 1 |
| 98 | + else: |
| 99 | + r -= 1 |
| 100 | + return returnList |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | +## Complexity |
| 105 | + |
| 106 | +- Time complexity: $$O(n^2)$$ |
| 107 | +<!-- Add time complexity here, e.g. $$O(n)$$ --> |
| 108 | + |
| 109 | +- Space complexity: $$O(n)$$ |
| 110 | +<!-- Add space complexity here, e.g. $$O(n)$$ --> |
| 111 | + |
0 commit comments