leetcode -day31 Subsets I II
2015-08-18 20:13
323 查看
1、
Subsets
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 =
a solution is:
分析:想到的方法是首先进行排序,从头到尾一次选择要不要该元素。能够递归实现。例如以下代码。
2、Subsets II
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 =
a solution is:
分析:此题和上题类似,就是有了反复元素,想法也是先进行排序,排序后,从头到尾遍历,记录每一个元素的个数,每一个子集中有0-i个指定元素(一共i个),代码例如以下:
Subsets
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], [] ]
分析:想到的方法是首先进行排序,从头到尾一次选择要不要该元素。能够递归实现。例如以下代码。
class Solution { public: vector<vector<int> >* v; vector<vector<int> > subsets(vector<int> &S) { v = new vector<vector<int> >(); //先排序 sort(S.begin(),S.end()); vector<int> res; generate(res, S, 0); return *v; } //对每个元素有放与不放两种选择 void generate(vector<int> res, vector<int> &S, int i) { if(i == S.size()) { v->push_back(res); return; } else { generate(res, S, i+1);//不放当前元素 res.push_back(S[i]); //放入当前元素 generate(res, S, i+1); } } };
2、Subsets II
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], [] ]
分析:此题和上题类似,就是有了反复元素,想法也是先进行排序,排序后,从头到尾遍历,记录每一个元素的个数,每一个子集中有0-i个指定元素(一共i个),代码例如以下:
class Solution { public: vector<vector<int> >* v; vector<vector<int> > subsetsWithDup(vector<int> &S) { v = new vector<vector<int> >(); //先排序 sort(S.begin(),S.end()); vector<int> res; generate(res, S, 0,0,0); return *v; } //pre: 排序后前一个元素 num: 前一个元素出现的次数 void generate(vector<int> res, vector<int> &S, int i,int pre,int num) { if(i == S.size()) { v->push_back(res); for(int j=1; j<=num; ++j){ res.push_back(pre); //放入之前元素 v->push_back(res); } return; } else if(pre != S[i] || num < 1 ) //与之前元素不同或者是首次 { if(num < 1){ generate(res,S,i+1,S[i],1); }else{ generate(res, S, i+1,S[i],1);//放入0个元素 for(int j=1; j<=num; ++j){ res.push_back(pre); generate(res, S, i+1,S[i],1);//放入i个元素后从当前位置開始 } } }else{ pre = S[i]; generate(res, S, i+1,pre,num+1); } } };
相关文章推荐
- Autofac 的构造函数注入方式
- NBUT 1220 SPY
- 总线设备驱动模型——设备篇
- Unable to execute dex: Multiple dex files define Lorg/ap (
- 如何激活一个window/dialog && 不能直接对Dialog Box使用SetFocus
- [algorithm] graph algorithm
- N-Queens N皇后问题 DFS
- leetcode之路026 Remove Duplicates from Sorted Arrayy
- dinic 模板
- 斯特林数 组合数
- HDU 5399
- Codeforces Gym 100650C The Game of Efil DFS
- Vs2010 配置驱动的开发环境
- iOS视图控制器的跳转方法
- java和javascript双引号嵌套的问题
- 线段树的两种查询方式
- 分词器之NLPIR加密文件在哪
- 微信支付又下一城:可以缴税了
- android---复杂布局的学习
- 动态行转列小记