g0001_0100.s0018_4sum.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0018_4sum;
// #Medium #Array #Sorting #Two_Pointers #2023_08_09_Time_3_ms_(99.77%)_Space_43.9_MB_(82.30%)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 18 - 4Sum\.
*
* Medium
*
* Given an array `nums` of `n` integers, return _an array of all the **unique** quadruplets_ `[nums[a], nums[b], nums[c], nums[d]]` such that:
*
* * `0 <= a, b, c, d < n`
* * `a`, `b`, `c`, and `d` are **distinct**.
* * `nums[a] + nums[b] + nums[c] + nums[d] == target`
*
* You may return the answer in **any order**.
*
* **Example 1:**
*
* **Input:** nums = [1,0,-1,0,-2,2], target = 0
*
* **Output:** [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
*
* **Example 2:**
*
* **Input:** nums = [2,2,2,2,2], target = 8
*
* **Output:** [[2,2,2,2]]
*
* **Constraints:**
*
* * `1 <= nums.length <= 200`
* * -109 <= nums[i] <= 109
* * -109 <= target <= 109
**/
@SuppressWarnings("java:S135")
public class Solution {
public List> fourSum(int[] nums, int target) {
int n = nums.length;
Arrays.sort(nums);
List> result = new ArrayList<>();
for (int i = 0; i < n - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
if ((long) nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) {
break;
}
if ((long) nums[i] + nums[n - 3] + nums[n - 2] + nums[n - 1] < target) {
continue;
}
for (int j = i + 1; j < n - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
if ((long) nums[j] + nums[j + 1] + nums[j + 2] > target - nums[i]) {
break;
}
if ((long) nums[j] + nums[n - 2] + nums[n - 1] < target - nums[i]) {
continue;
}
int tempTarget = target - (nums[i] + nums[j]);
int low = j + 1;
int high = n - 1;
while (low < high) {
int curSum = nums[low] + nums[high];
if (curSum == tempTarget) {
List tempList = new ArrayList<>();
tempList.add(nums[i]);
tempList.add(nums[j]);
tempList.add(nums[low]);
tempList.add(nums[high]);
result.add(tempList);
low++;
high--;
while (low < high && nums[low] == nums[low - 1]) {
low++;
}
while (low < high && nums[high] == nums[high + 1]) {
high--;
}
} else if (curSum < tempTarget) {
low++;
} else {
high--;
}
}
}
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy