90. 子集 II
排序!!
同组合三,两种去重方法:
- 使用used数组,判断前一数字是否重复
- i>startIndex&&nums[i]==nums[i-1]
class Solution {
public:
void backtracking(vector<int>& nums,int startIndex)
{
result.push_back(path);
for(int i=startIndex;i<nums.size();i++)
{
//去重
if(i>startIndex&&nums[i]==nums[i-1])
continue;
path.push_back(nums[i]);
backtracking(nums,i+1);
path.pop_back();
}
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(),nums.end());
backtracking(nums,0);
return result;
}
vector<int> path;
vector<vector<int>> result;
};