Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
For
example,
If S = [1,2,2]
,
a solution is:
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 |
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; } }; |
原文:http://www.cnblogs.com/winscoder/p/3535429.html