Leetcode 90. Subsets II

Posted zhangwj0101

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 90. Subsets II相关的知识,希望对你有一定的参考价值。

Question

Given a collection of integers that might contain duplicates, nums, return all possible subsets.

Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2], a solution is:

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]

Code

public List<List<Integer>> subsetsWithDup(int[] nums) 
        Arrays.sort(nums);
        Set<List<Integer>> sresults = new HashSet<>();
        List<List<Integer>> results = new ArrayList<>();
        int len = (int) Math.pow(2, nums.length);
        boolean[] flags = new boolean[nums.length];
        for (int i = 0; i < len; i++) 
            for (int j = 0; j < flags.length; j++) 
                if (!flags[j]) 
                    flags[j] = true;
                    break;
                
                flags[j] = false;
            
            List<Integer> sub = new ArrayList<>();
            for (int j = 0; j < flags.length; j++) 
                if (flags[j]) 
                    sub.add(nums[j]);
                
            
            sresults.add(sub);
        
        for (List<Integer> sub : sresults) 
            results.add(sub);
        
        return results;
    

以上是关于Leetcode 90. Subsets II的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 90. Subsets II 子集合 II

Leetcode 90. Subsets II

[leetcode-90-Subsets II]

Leetcode 90: Subsets II

Leetcode 90. Subsets II

LeetCode 90. Subsets II