您的位置:首页 > 其它

LeetCode (Subsets II)

2017-05-24 17:49 316 查看
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 = 
[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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: