去重逻辑:
关键是画出递归树!当我们即将进入第二个2的递归的时候,发现isVisit数组是100,也就是说这俩重复的数是False,并且这俩在nums值相同,所以写出去重逻辑!
class Solution {
public:vector<int>* pNums,*pArr=new vector<int>();vector<vector<int>> ans;vector<bool>* isVisit;vector<vector<int>> subsetsWithDup(vector<int>& nums) {sort(nums.begin(),nums.end());pNums=&nums;isVisit=new vector<bool>(nums.size(),0);ans.push_back(vector<int>());dfs(0);return ans;}void dfs(int idx){if(idx>=pNums->size()){return;}for(int i=idx;i<pNums->size();++i){if(i>=1 && (*pNums)[i-1]==(*pNums)[i] && (*isVisit)[i-1]==0 && (*isVisit)[i]==0){ continue;}if(!((*isVisit)[i])){pArr->push_back((*pNums)[i]);ans.push_back(*pArr);(*isVisit)[i]=1;}dfs(i+1);pArr->pop_back();(*isVisit)[i]=0;}}
};