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-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0018_4sum;
// #Medium #Array #Sorting #Two_Pointers #2022_02_17_Time_24_ms_(60.61%)_Space_45.2_MB_(29.06%)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
@SuppressWarnings("java:S135")
public class Solution {
public List> fourSum(int[] nums, int target) {
List> list = new ArrayList<>();
int i;
int j;
int k;
int l;
Arrays.sort(nums);
for (i = 0; i < nums.length - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for (j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
k = j + 1;
l = nums.length - 1;
while (k < l) {
int sum = nums[i] + nums[j] + nums[k] + nums[l];
if (sum == target) {
ArrayList l1 = new ArrayList<>();
l1.add(nums[i]);
l1.add(nums[j]);
l1.add(nums[k]);
l1.add(nums[l]);
list.add(l1);
l--;
if (k < l && nums[l] == nums[l + 1]) {
l--;
}
} else if (sum > target) {
l--;
} else {
k++;
}
}
}
}
list = new ArrayList<>(new LinkedHashSet<>(list));
return list;
}
}