本文共 670 字,大约阅读时间需要 2 分钟。
题目:
解答:
在Subsets的基础上,加一个结果中是否存在该元素的判断。
代码:
class Solution { public: vector> subsetsWithDup(vector &S) { vector > res; vector temp; int n = S.size(); if (n == 0) return res; sort(S.begin(), S.end()); search(0, n, res, temp, S); return res; } void search(int k, int n, vector > &res, vector &temp, vector &S) { if (k == n) { if (find(res.begin(), res.end(), temp) == res.end()) res.push_back(temp); return; } temp.push_back(S[k]); search(k + 1, n, res, temp, S); temp.pop_back(); search(k + 1, n, res, temp, S); } };
转载地址:http://kytsi.baihongyu.com/