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], [] ]
即,S中的数字可以重复,因此得到的子数组有些可能会重复 要剔除重复的
class Solution {
public:
void dfs(vector<int> &S,vector<int> &temp,int len,int pos)
{
res.push_back(temp);
for(int i=pos;i<len;++i)
{
//重点在这里!!!!!不重复~!
if(i>pos && S[i]==S[i-1])
continue;
temp.push_back(S[i]);
dfs(S,temp,len,i+1);
temp.pop_back();
}
}
vector<vector<int> > subsetsWithDup(vector<int> &S) {
res.clear();
vector<int> inum;
int len = S.size();
if(len==0)
return res;
//排序,可以使重复的数字在一起
sort(S.begin(),S.end());
dfs(S,inum,len,0);
return res;
}
vector<vector<int> > res;
};
博客围绕给定可能含重复整数的集合S,求解其所有可能子集展开。要求子集中元素为非降序排列,且解集不能包含重复子集。如集合[1,2,2],需剔除重复的子数组。
731

被折叠的 条评论
为什么被折叠?



