Subsets

来源:互联网 发布:windows me u盘安装 编辑:程序博客网 时间:2024/06/05 19:35

Given a set of distinct integers, 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,3], a solution is:

[  [3],  [1],  [2],  [1,2,3],  [1,3],  [2,3],  [1,2],  []

]

void visit(vector<int> &S, int n, int target, int count, int pos, vector<int> &a, vector<vector<int> > &result){if (count == target){result.push_back(a);return;}if (pos == n){return;}a.push_back(S[pos]);visit(S, n, target, count+1, pos+1, a, result);a.pop_back();visit(S, n, target, count, pos+1, a, result);}vector<vector<int> > subsets(vector<int> &S) {int n = S.size();vector<vector<int> > result;vector<int> a;result.push_back(a);for (int i = 1; i <= n; i++){visit(S, n, i, 0, 0, a, result);}return result;}


0 0