Subsets
来源:互联网 发布:pc1500 水利计算软件 编辑:程序博客网 时间:2024/04/30 15:16
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], []]
这个题目和上一个combinations题目一样的,这个题目只是在进行下一个level之前把当前的level的结果push上去而已。比较tricky的有这么几个地方:
1. 需要sort一下来确保是non-descending
2. 不管怎样都要push一个空的上去。
其他的和combinations 那个题目一样。
class Solution {public: vector<vector<int> > subsets(vector<int> &S) { vector< vector<int> > res; vector<int> solution; res.push_back(solution); // non data if (S.size()==0) return res; sort(S.begin(),S.end()); // make sure it is ascending but not descending helper(res,solution, S, 0); //backtracking return res; } void helper(vector< vector<int> >& res, vector<int>& solution, vector<int>& input, int level){ for (int i=level; i<input.size();i++){ solution.push_back(input[i]); res.push_back(solution); helper(res,solution,input,i+1); solution.pop_back(); } }};
0 0
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- codeforces 515c Drazil and Factorial 分类讨论
- uva 10803(floyd变式)
- Failed to start LSB: Bring up/down networking
- 初中生可能学好编程么?
- codeforces 515d Drazil and Tiles
- Subsets
- Java中的TreeMap、Comparable、Comparator
- 历届试题 兰顿蚂蚁
- codevs1082 线段树练习3
- 《数据结构编程实验》 1.5.4A Contesting Decision
- oracle中解决被锁定的scott用户的方法。
- 【Unity】查看、使用Untiy内部库和方法
- Ajax使用范例
- Java与数据库对应的日期类型