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

g0001_0100.s0090_subsets_ii.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g0001_0100.s0090_subsets_ii;

// #Medium #Array #Bit_Manipulation #Backtracking
// #2022_02_21_Time_2_ms_(75.68%)_Space_44.3_MB_(14.12%)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SuppressWarnings("java:S5413")
public class Solution {
    List> allComb = new ArrayList<>();
    List comb = new ArrayList<>();
    int[] nums;

    public List> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        this.nums = nums;
        dfs(0);
        allComb.add(new ArrayList<>());
        return allComb;
    }

    private void dfs(int start) {
        if (start > nums.length) {
            return;
        }
        for (int i = start; i < nums.length; i++) {
            if (i > start && nums[i] == nums[i - 1]) {
                continue;
            }
            comb.add(nums[i]);
            allComb.add(new ArrayList<>(comb));
            dfs(i + 1);
            comb.remove(comb.size() - 1);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy