Skip to content

Commit 4a18acc

Browse files
authored
Merge pull request #6 from xchux/feature/15-3Sum
✨ (solution): Problem 15. 3Sum solution
2 parents 7b3ba7a + 835f663 commit 4a18acc

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

solutions/15. 3Sum/README.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+

solutions/15. 3Sum/Solution.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public List<List<Integer>> threeSum(int[] nums) {
3+
List<List<Integer>> res = new ArrayList<>();
4+
Arrays.sort(nums);
5+
for (int k = 0; k < nums.length - 2; k++) {
6+
if (k > 0 && nums[k] == nums[k - 1])
7+
continue;
8+
int i = k + 1, j = nums.length - 1;
9+
while (i < j) {
10+
int sum = nums[k] + nums[i] + nums[j];
11+
if (sum < 0) {
12+
i++;
13+
} else if (sum > 0) {
14+
j--;
15+
} else {
16+
res.add(Arrays.asList(nums[k], nums[i], nums[j]));
17+
while (i < j && nums[i] == nums[i + 1])
18+
i++;
19+
while (i < j && nums[j] == nums[j - 1])
20+
j--;
21+
i++;
22+
j--;
23+
}
24+
}
25+
}
26+
return res;
27+
}
28+
}

solutions/15. 3Sum/Solution.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def threeSum(self, nums: list[int]) -> list[list[int]]:
3+
nums.sort()
4+
n = len(nums)
5+
returnList = []
6+
for i, i_num in enumerate(nums):
7+
if i and i_num == nums[i-1]:
8+
continue
9+
l = i + 1
10+
r = n - 1
11+
while l < r:
12+
sum = i_num + nums[l] + nums[r]
13+
if sum == 0:
14+
returnList.append([i_num, nums[l], nums[r]])
15+
while l < r and nums[l] == nums[l+1]:
16+
l += 1
17+
while l < r and nums[r] == nums[r-1]:
18+
r -= 1
19+
l += 1
20+
r -= 1
21+
elif sum < 0:
22+
l += 1
23+
else:
24+
r -= 1
25+
return returnList

0 commit comments

Comments
 (0)