Package g0001_0100.s0015_3sum
Class Solution
java.lang.Object
g0001_0100.s0015_3sum.Solution
15 - 3Sum.
Medium
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Example 1:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Example 2:
Input: nums = []
Output: []
Example 3:
Input: nums = [0]
Output: []
Constraints:
0 <= nums.length <= 3000-105 <= nums[i] <= 105
To solve the 3Sum problem in Java using a Solution class, we’ll follow these steps:
- Define a
Solutionclass with a method namedthreeSumthat takes an array of integersnumsas input and returns a list of lists representing the triplets that sum up to zero. - Sort the input array
numsto ensure that duplicate triplets are avoided. - Initialize an empty list
resultto store the triplets. - Iterate over the elements of the sorted array
numsup to the second to last element. - Within the outer loop, initialize two pointers,
leftandright, whereleftstarts at the next element after the current element andrightstarts at the last element of the array. - While
leftis less thanright, check if the sum of the current element (nums[i]),nums[left], andnums[right]equals zero. - If the sum is zero, add
[nums[i], nums[left], nums[right]]to theresultlist. - Move the
leftpointer to the right (incrementleft) and therightpointer to the left (decrementright). - If the sum is less than zero, increment
left. - If the sum is greater than zero, decrement
right. - After the inner loop finishes, increment the outer loop index while skipping duplicates.
- Return the
resultlist containing all the valid triplets.
Here’s the implementation:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue; // Skip duplicates
}
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == 0) {
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
// Skip duplicates
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
left++;
right--;
} else if (sum < 0) {
left++;
} else {
right--;
}
}
}
return result;
}
public static void main(String[] args) {
Solution solution = new Solution();
// Test cases
int[] nums1 = {-1, 0, 1, 2, -1, -4};
System.out.println("Example 1 Output: " + solution.threeSum(nums1));
int[] nums2 = {};
System.out.println("Example 2 Output: " + solution.threeSum(nums2));
int[] nums3 = {0};
System.out.println("Example 3 Output: " + solution.threeSum(nums3));
}
}
This implementation provides a solution to the 3Sum problem in Java.
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
threeSum
-