标签:sub amp ras sort public false 最简 for style
方法:最简单的是使用subset的方法,然后去除重复即可,这里需要对原始的vector先进行排序
class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { vector<vector<int>> result; sort(nums.begin(), nums.end()); vector<bool> select(nums.size(), false); subsets(nums, select, 0, result); sort(result.begin(), result.end()); vector<vector<int>>::iterator it = unique(result.begin(), result.end()); result.erase(it, result.end()); return result; } private: void subsets(const vector<int> &nums, vector<bool> &select, int step, vector<vector<int>> &result) { if(step == nums.size()) { vector<int> tmp; for(int i=0; i<nums.size(); ++i) if(select[i]) tmp.push_back(nums[i]); result.push_back(tmp); return; } select[step] = true; subsets(nums, select, step+1, result); select[step] = false; subsets(nums, select, step+1, result); } };
标签:sub amp ras sort public false 最简 for style
原文地址:http://www.cnblogs.com/chengyuz/p/6820846.html