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>> subsetsWithDup(vector<int>& nums) {
 5         vector<vector<int>> ans(1);
 6         sort(nums.begin(), nums.end());
 7         for(int i = 0; i < nums.size(); i++){
 8             int len = ans.size();
 9             for(int j = 0; j < len; j++){
10                 ans.push_back(ans[j]);
11                 ans.back().push_back(nums[i]);
12             }
13         }
14         sort(ans.begin(), ans.end());
15         ans.erase(unique(ans.begin(), ans.end()), ans.end());
16         return ans;
17     }
18 };

递归算法:

 1 #include<algorithm>
 2 class Solution {
 3 public:
 4     void dfs(vector<int> nums, vector<int>& temp, vector<vector<int>>& ans, int begin){
 5         ans.push_back(temp);
 6         for(int i = begin; i < nums.size(); i++){
 7             if(i==begin||nums[i]!=nums[i-1]){//dfs中常有这种结构,先添加,在递归,最后再复原
 8                 temp.push_back(nums[i]);
 9                 dfs(nums, temp, ans, i+1);
10                 temp.pop_back();
11             }
12         }
13     }
14     vector<vector<int>> subsetsWithDup(vector<int>& nums) {
15         vector<vector<int>> ans;
16         vector<int> temp;
17         sort(nums.begin(), nums.end());
18         dfs(nums, temp, ans, 0);
19         return ans;
20     }
21 };

原文地址:https://www.cnblogs.com/mr-stn/p/8997775.html

时间: 2024-07-30 17:31:08

leetcode 90. 子集 II的相关文章

[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. 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 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.子集

子集 给定一个可能包含重复元素的整数数组 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

[LeetCode] 90.Subsets II tag: backtracking

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. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 这个题目思路

力扣90——子集 II

原题 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 原题url:https://leetcode-cn.com/problems/subsets-ii/ 解题 递归 这道题,针对已经刷了不少题目的我们而言,应该第一想到的就是递归了,从第1个数开始,每次遍历1个数,如果和之前的数相同则跳过,然后以下一个数为起点,

leetcode 90 Subsets II ----- java

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], [] ] 这道题就是Sub