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], [] ]
hints:这道题目先对其排序,然后还是用DFS去做。
class Solution {
private:
vector<vector<int> > ret;
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(S.begin(),S.end());
ret.clear();
ret.push_back(vector<int> ());
vector<int> cur;
DFS(S,S.size(),0,cur);
return vector<vector<int> >(ret.begin(),ret.end());
}
void DFS(vector<int> S, int n, int start , vector<int>& cur){
if(start==n)
return;
for(int i=start ; i<n ; i++){
if(i!=start && S[i]==S[i-1])
continue;
cur.push_back(S[i]);
ret.push_back(cur);
DFS(S,n,i+1,cur);
cur.pop_back();
}
}
};