[LeetCode] 90. Subsets II
grandyang opened this issue · comments
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]
, a solution is:
[
[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到当前子集的个数,若相同,则新子集个数减去之前循环时子集的个数当做起点来循环,这样就不会产生重复了,代码如下:
解法一:
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int> &S) {
if (S.empty()) return {};
vector<vector<int>> res(1);
sort(S.begin(), S.end());
int size = 1, last = S[0];
for (int i = 0; i < S.size(); ++i) {
if (last != S[i]) {
last = S[i];
size = res.size();
}
int newSize = res.size();
for (int j = newSize - size; j < newSize; ++j) {
res.push_back(res[j]);
res.back().push_back(S[i]);
}
}
return res;
}
};
整个添加的顺序为:
[]
[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的叶节点,因为它们是重复的子集,应被抛弃。代码如下:
解法二:
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int> &S) {
if (S.empty()) return {};
vector<vector<int>> res;
vector<int> out;
sort(S.begin(), S.end());
getSubsets(S, 0, out, res);
return res;
}
void getSubsets(vector<int> &S, int pos, vector<int> &out, vector<vector<int>> &res) {
res.push_back(out);
for (int i = pos; i < S.size(); ++i) {
out.push_back(S[i]);
getSubsets(S, i + 1, out, res);
out.pop_back();
while (i + 1 < S.size() && S[i] == S[i + 1]) ++i;
}
}
};
整个添加的顺序为:
[]
[1]
[1 2]
[1 2 2]
[2]
[2 2]
类似题目:
参考资料:
https://leetcode.com/problems/subsets-ii/
https://leetcode.com/problems/subsets-ii/discuss/30137/Simple-iterative-solution
https://leetcode.com/problems/subsets-ii/discuss/30168/C%2B%2B-solution-and-explanation