30 Day Challenge Day 14 | Leetcode 90. Subsets II
题解
Medium | Backtracking
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> sols;
vector<int> sol;
helper(nums, 0, sol, sols);
return sols;
}
void helper(vector<int>& nums, int pos, vector<int>& sol, vector<vector<int>>& sols) {
sols.push_back(sol);
for(int i = pos; i < nums.size(); i++) {
if(i > pos && nums[i] == nums[i-1]) continue;
sol.push_back(nums[i]);
helper(nums, i+1, sol, sols);
sol.pop_back();
}
}
};