Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If S = [1,2,2], a solution is:
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
Code:
class Solution {
public:
void findSub(vector<int> &S,int level,vector<int> &buf,vector<vector<int>> &res){
for(int i=level;i<S.size();i++){
buf.push_back(S[i]);
res.push_back(buf);
if(i<S.size()-1)
findSub(S,i+1,buf,res);
buf.pop_back();
while(i<S.size()-1&&S[i]==S[i+1])
i++;
}
}
vector<vector<int> > subsetsWithDup(vector<int> &S) {
vector<vector<int>> res;
vector<int> buf;
res.push_back(buf);
sort(S.begin(),S.end());
findSub(S,0,buf,res);
return res;
}
};

浙公网安备 33010602011771号