原题链接:90. 子集 II
solution:
回溯+去重
class Solution {
public:
vector<vector<int>> res; //定义返回值
vector<int> path;
void dfs(int u, vector<int> &nums) {
if(u == nums.size()) {
res.push_back(path);
return;
}
res.push_back(path);
for(int i = u;i < nums.size();i++) {
if(i > u && nums[i] == nums[i - 1]) continue;
path.push_back(nums[i]);
dfs(i + 1, nums);
path.pop_back();
}
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(),nums.end());
dfs(0, nums);
return res;
}
};