LeetCode(90):子集 II

Medium!

题目描述:

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

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

示例:

输入: [1,2,2]
输出:
[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

解题思路:

这道子集合之二是之前那道Subsets 子集合 的延伸,这次输入数组允许有重复项,其他条件都不变,只需要在之前那道题解法的基础上稍加改动便可以做出来,我们先来看非递归解法,拿题目中的例子[1 2 2]来分析,根据之前Subsets 子集合 里的分析可知,当处理到第一个2时,此时的子集合为[], [1], [2], [1, 2],而这时再处理第二个2时,如果在[]和[1]后直接加2会产生重复,所以只能在上一个循环生成的后两个子集合后面加2,发现了这一点,题目就可以做了,我们用last来记录上一个处理的数字,然后判定当前的数字和上面的是否相同,若不同,则循环还是从0到当前子集的个数,若相同,则新子集个数减去之前循环时子集的个数当做起点来循环,这样就不会产生重复了。

C++解法一:

 1 class Solution {
 2 public:
 3     vector<vector<int>> subsetsWithDup(vector<int> &S) {
 4         if (S.empty()) return {};
 5         vector<vector<int>> res(1);
 6         sort(S.begin(), S.end());
 7         int size = 1, last = S[0];
 8         for (int i = 0; i < S.size(); ++i) {
 9             if (last != S[i]) {
10                 last = S[i];
11                 size = res.size();
12             }
13             int newSize = res.size();
14             for (int j = newSize - size; j < newSize; ++j) {
15                 res.push_back(res[j]);
16                 res.back().push_back(S[i]);
17             }
18         }
19         return res;
20     }
21 };

整个添加的顺序为:

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

对于递归的解法,根据之前Subsets 子集合 里的构建树的方法,在处理到第二个2时,由于前面已经处理了一次2,这次我们只在添加过2的[2] 和 [1 2]后面添加2,其他的都不添加,那么这样构成的二叉树如下图所示:

                        []
                   /          \
                  /            \
                 /                            [1]                []
           /       \           /              /         \         /      \
       [1 2]       [1]       [2]     []
      /     \     /   \     /   \    /   [1 2 2] [1 2]  X   [1]  [2 2] [2] X  []

代码只需在原有的基础上增加一句话,while (S[i] == S[i + 1]) ++i; 这句话的作用是跳过树中为X的叶节点,因为它们是重复的子集,应被抛弃。

C++解法二:

 1 class Solution {
 2 public:
 3     vector<vector<int>> subsetsWithDup(vector<int> &S) {
 4         if (S.empty()) return {};
 5         vector<vector<int>> res;
 6         vector<int> out;
 7         sort(S.begin(), S.end());
 8         getSubsets(S, 0, out, res);
 9         return res;
10     }
11     void getSubsets(vector<int> &S, int pos, vector<int> &out, vector<vector<int>> &res) {
12         res.push_back(out);
13         for (int i = pos; i < S.size(); ++i) {
14             out.push_back(S[i]);
15             getSubsets(S, i + 1, out, res);
16             out.pop_back();
17             while (i + 1 < S.size() && S[i] == S[i + 1]) ++i;
18         }
19     }
20 };

整个添加的顺序为:

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

原文地址:https://www.cnblogs.com/ariel-dreamland/p/9159492.html

时间: 2024-10-18 14:48:34

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