Skip to content

Commit 8f98353

Browse files
authored
Enhance README with 3Sum problem solution
Added detailed explanation and implementation for the 3Sum problem in Java, including constraints and step-by-step approach.
1 parent 9f5fddc commit 8f98353

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

src/main/java/g0001_0100/s0015_3sum/readme.md

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,83 @@ Notice that the solution set must not contain duplicate triplets.
3333
**Constraints:**
3434

3535
* `3 <= nums.length <= 3000`
36-
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
36+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
37+
38+
To solve the 3Sum problem in Java using a `Solution` class, we'll follow these steps:
39+
40+
1. Define a `Solution` class with a method named `threeSum` that takes an array of integers `nums` as input and returns a list of lists representing the triplets that sum up to zero.
41+
2. Sort the input array `nums` to ensure that duplicate triplets are avoided.
42+
3. Initialize an empty list `result` to store the triplets.
43+
4. Iterate over the elements of the sorted array `nums` up to the second to last element.
44+
5. Within the outer loop, initialize two pointers, `left` and `right`, where `left` starts at the next element after the current element and `right` starts at the last element of the array.
45+
6. While `left` is less than `right`, check if the sum of the current element (`nums[i]`), `nums[left]`, and `nums[right]` equals zero.
46+
7. If the sum is zero, add `[nums[i], nums[left], nums[right]]` to the `result` list.
47+
8. Move the `left` pointer to the right (increment `left`) and the `right` pointer to the left (decrement `right`).
48+
9. If the sum is less than zero, increment `left`.
49+
10. If the sum is greater than zero, decrement `right`.
50+
11. After the inner loop finishes, increment the outer loop index while skipping duplicates.
51+
12. Return the `result` list containing all the valid triplets.
52+
53+
Here's the implementation:
54+
55+
```java
56+
import java.util.ArrayList;
57+
import java.util.Arrays;
58+
import java.util.List;
59+
60+
public class Solution {
61+
public List<List<Integer>> threeSum(int[] nums) {
62+
Arrays.sort(nums);
63+
List<List<Integer>> result = new ArrayList<>();
64+
65+
for (int i = 0; i < nums.length - 2; i++) {
66+
if (i > 0 && nums[i] == nums[i - 1]) {
67+
continue; // Skip duplicates
68+
}
69+
70+
int left = i + 1;
71+
int right = nums.length - 1;
72+
73+
while (left < right) {
74+
int sum = nums[i] + nums[left] + nums[right];
75+
if (sum == 0) {
76+
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
77+
78+
// Skip duplicates
79+
while (left < right && nums[left] == nums[left + 1]) {
80+
left++;
81+
}
82+
while (left < right && nums[right] == nums[right - 1]) {
83+
right--;
84+
}
85+
86+
left++;
87+
right--;
88+
} else if (sum < 0) {
89+
left++;
90+
} else {
91+
right--;
92+
}
93+
}
94+
}
95+
96+
return result;
97+
}
98+
99+
public static void main(String[] args) {
100+
Solution solution = new Solution();
101+
102+
// Test cases
103+
int[] nums1 = {-1, 0, 1, 2, -1, -4};
104+
System.out.println("Example 1 Output: " + solution.threeSum(nums1));
105+
106+
int[] nums2 = {};
107+
System.out.println("Example 2 Output: " + solution.threeSum(nums2));
108+
109+
int[] nums3 = {0};
110+
System.out.println("Example 3 Output: " + solution.threeSum(nums3));
111+
}
112+
}
113+
```
114+
115+
This implementation provides a solution to the 3Sum problem in Java.

0 commit comments

Comments
 (0)