标签:
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], [] ].
依然回溯,补充一个条件即可:
void subsets(vector<int> vecInt) { sort(vecInt.begin(), vecInt.end()); vector<int> vecWorker; subsetsHelper(vecWorker, vecInt, 0); } void subsetsHelper(vector<int>& vecWorker, const vector<int>& vecInt, int pos) { outputToResult(vecWorker); for (int i = pos; i < vecInt.length(); i++) { if (i != pos && vecInt[i] == vecInt[i-1]) continue; vecWorker.push_back(vecInt[i]); subsetsHelper(vecWorker, vecInt, i+1); vecWorker.pop_back(); } }
标签:
原文地址:http://www.cnblogs.com/litao-tech/p/4178620.html