All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0018_4sum.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy