本文共 1986 字,大约阅读时间需要 6 分钟。
class Solution {//because Elements in a subset must be in non-descending order.//so if we sort the vector first.//If we choose an elem[i] into subset, then we should choose elem[j](i
second time
class Solution {public: void subsetUtil(vector & S, vector& used, int curIdx, vector & curPath, vector >& allPath) { if(curIdx == S.size()) { allPath.push_back(curPath); return ; } subsetUtil(S, used, curIdx+1, curPath, allPath); if(curIdx >= 1 && S[curIdx] == S[curIdx-1] && used[curIdx-1] == false) return; used[curIdx] = true; curPath.push_back(S[curIdx]); subsetUtil(S, used, curIdx+1, curPath, allPath); curPath.pop_back(); used[curIdx] = false; } vector > subsetsWithDup(vector &S) { // Start typing your C/C++ solution below // DO NOT write int main() function sort(S.begin(), S.end()); vector used(S.size(), false); vector > allPath; vector curPath; subsetUtil(S, used, 0, curPath, allPath); return allPath; }};
转载地址:http://eqxti.baihongyu.com/