LeetCode (Subsets II)
2017-05-24 17:49
330 查看
Problem:
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 =
Solution:
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
if(nums.empty()) return {{}};
sort(nums.begin(), nums.end());
vector<vector<int>> ans;
int n = nums.back();
nums.pop_back();
vector<vector<int>> pre = subsetsWithDup(nums);
for(int i = 0; i < pre.size(); i++){
ans.push_back(pre[i]);
vector<int> temp = pre[i];
temp.push_back(n);
if(find(pre.begin(), pre.end(), temp) == pre.end())
ans.push_back(temp);
}
return ans;
}
};
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], [] ]
Solution:
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
if(nums.empty()) return {{}};
sort(nums.begin(), nums.end());
vector<vector<int>> ans;
int n = nums.back();
nums.pop_back();
vector<vector<int>> pre = subsetsWithDup(nums);
for(int i = 0; i < pre.size(); i++){
ans.push_back(pre[i]);
vector<int> temp = pre[i];
temp.push_back(n);
if(find(pre.begin(), pre.end(), temp) == pre.end())
ans.push_back(temp);
}
return ans;
}
};
相关文章推荐
- leetcode:Subsets II
- 80 leetcode - Subsets II
- 【leetcode】【90】Subsets II
- Leetcode之Subsets II 问题
- 【LeetCode】Subsets II
- LeetCode-Subsets II
- [LeetCode] Subsets II 解题报告
- [leetcode]Subsets II
- [LeetCode105]Subsets II
- [leetcode]Subsets II
- leetcode: Subsets II
- leetcode-90-Subsets II
- Subsets II -- LeetCode
- 【Leetcode】【Medium】Subsets II
- ***LeetCode 90 Subsets ii
- LeetCode *** 90. Subsets II
- [LeetCode]Subsets II
- [leetcode]Subsets II
- LeetCode(90) Subsets II
- LeetCode - Subsets II