Leetcode 90.子集

子集

给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

说明:解集不能包含重复的子集。

示例:

输入: [1,2,2]

输出:

[

[2],

[1],

[1,2,2],

[2,2],

[1,2],

[]

]

 1 import java.util.ArrayList;
 2 import java.util.Arrays;
 3 import java.util.List;
 4
 5 class Solution {
 6     public List<List<Integer>> subsetsWithDup(int[] nums) {
 7         List<List<Integer>> list = new ArrayList<>();
 8         if(nums == null || nums.length == 0){
 9             return list;
10         }
11         List<Integer> data = new ArrayList<>();
12         int length = nums.length;
13         boolean[] visit = new boolean[length];
14         list.add(data);
15         Arrays.sort(nums);
16         dfs(list,data,length,nums,0,visit);
17         return list;
18     }
19
20     void dfs(List<List<Integer>> list,List<Integer> data,int length,int[] nums,int start,boolean[] visit){
21         for(int i = start;i < length;i++){
22             if(!visit[i]){
23                 if(i > 0 && nums[i] == nums[i - 1] && !visit[i - 1]){
24                     continue;
25                 }
26                 data.add(nums[i]);
27                 visit[i] = true;
28                 list.add(new ArrayList<>(data));
29                 dfs(list,data,length,nums,i + 1,visit);
30                 visit[i] = false;
31                 data.remove(data.size() - 1);
32             }
33         }
34     }
35 }

 

原文地址:https://www.cnblogs.com/kexinxin/p/10163071.html

时间: 2024-07-30 17:26:00

Leetcode 90.子集的相关文章

[leetcode] 90. 子集 II.md

90. 子集 II 78. 子集题的扩展,其中的元素可能会出现重复了 我们仍沿用78题的代码,稍作改动即可: 此时需要对nums先排个序,方便我们后面跳过选取相同的子集. 跳过选取相同的子集.当选取完第i个数时,如果后面的数,和当前数相同,就跳过,不必对其进行递归了. class Solution { private void dfs(int n, int k, int last, int[] nums, List<Integer> cur, List<List<Integer&g

leetcode 90. 子集 II

给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]思路和上一题类似,这里在最后把多余的排列除去即可,在有重复元素的nums中,要对其排序,再进行,处理,否则会出错 1 #include<algorithm> 2 class Solution { 3 public: 4 vector<vector<int

LeetCode --- 90. Subsets II

题目链接:Subsets II Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2]

Leetcode:Subsets 子集生成

Subsets: Given a set of distinct integers, S, 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 S = [1,2,3], a solution is: [ [3], [1], [2], [1

LeetCode 90. Subsets II (子集合之二)

Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: 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], [] ] 题目标签:Arr

[leetcode]90. Subsets II数组子集(有重)

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 题意: 是的[leetcode

leetCode 90.Subsets II(子集II) 解题思路和方法

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 soluti

[Leetcode 90]求含有重复数的子集 Subset II

[题目] Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. [思路] 注意sort,使得判断临接元素是否相邻. 与leetcode78类似,多了一个重复数判断条件 if(i>flag&&nums

leetcode 90. subsets

解题思路: 要生成子集,对于vector 中的每个数,对于每个子集有两种情况,加入或不加入. 因此代码: class Solution { public: void subsetG(vector<int> nums, vector<vector<int>>& result, vector<int> temp, int c) { if(c>=nums.size()) { result.push_back(temp); return ; } int