第一种方法:
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int>> vec({ {} });
int length = nums.size();
if (length == 0) return vec;
sort(nums.begin(),nums.end());
int i = vec.size();
int count = 0;
while (count < length) {
for (int j = 0; j < i; j++) {
vector<int> tmp = vec[j];
tmp.push_back(nums[count]);
int k = 0;
for(;k<vec.size();k++)
if(tmp == vec[k])
break;
if(k == vec.size())
vec.push_back(tmp);
}
i = vec.size();
count++;
/*
if (nums[count] == nums[count - 1]) {
i = vec.size();
count++;
continue;
}
*/
}
return vec;
}
};
分析:运行速度很慢,没有经过优化