LeetCode_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],
[]
]
DFS 的简单应用 : 求组合
class Solution {
public:
void DFS(vector<int> &S, vector<int> &temp,int n, int size,int start)
{
if(n == size)
{
result.push_back(temp);
return ;
}
if(n > size)
return ;
for(int i = start; i< len ;i++)
{
if(flag[i] == false)
{
flag[i] = true;
temp.push_back(S[i]);
DFS(S, temp, n+, size,i+);
temp.pop_back();
flag[i] = false;
}
}
}
vector<vector<int> > subsets(vector<int> &S) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
result.clear();
len = S.size();
flag.resize(len,false);
vector<int> temp;
result.push_back(temp) ;
sort(S.begin(), S.end());
for(int i = ; i <= len ; i++)
DFS(S, temp,, i,);
return result;
}
private:
vector<vector<int> > result ;
vector<bool> flag;
int len;
};
解释下start,因为组合和排列不同,组合不考虑排序,所以必须给元素进入temp指定一个次序,这个规则定义就是通过start,这样保证temp是有序的,也就保证result中没有重复
重写后的代码:
class Solution {
public:
void DFS(vector<int> &S, int currentSize, int length, int currentPos, vector<int> &ans)
{
if(length == currentSize){
res.push_back(ans);
return;
}
for(int i = currentPos; i < S.size(); i++)
{
ans.push_back(S[i]);
DFS(S, currentSize, length + , i+, ans);
ans.pop_back();
}
}
vector<vector<int> > subsets(vector<int> &S) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(S.begin(), S.end());
res.clear();
vector<int> empt;
res.push_back(empt);
for(int i = ; i <= S.size(); i++)
{
vector<int> ans;
DFS(S, i, , , ans);
}
return res;
}
private:
vector<vector<int>> res;
};
LeetCode_Subsets的更多相关文章
- LeetCode_Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
随机推荐
- QuickReport多页打印
You use composite reports for this(TQrCompositeReport) on the quickreports tabTake a look in the Dem ...
- MySQL常用聚合函数
官方文档:Aggregate (GROUP BY) Functions Name Description AVG() Return the average value of the argument ...
- EXTJS4:在grid中加入合计行
extjs4很方便的实现简单的合计(针对在不分页的情况下): 它效果实现在:Ext.grid.feature.Summary这个类中 Ext.define('TestResult', { extend ...
- mv command:unable to remove target: Is a director
mv command:unable to remove target: Is a director This is somewhat simple as long as we understand t ...
- centos下httpd 启动失败的解决办法
[root@csit yang]# service httpd start Starting httpd: [FA ...
- zoj2112
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112 经典的动态区间第K大. 用树状数组套线段树. 对原数组建一个树 ...
- Windows 8.1下使用IE 64位
Internet Options -> Advanced -> Settings Security组 对Enable 64-bit processes for Enhanced Prote ...
- PHP关于时区问题
最近在学习PHP过程中发现PHP中的格式化时间戳比北京时间晚了8个小时,上网搜索发现原来是时区不对,解决办法是: 1.永久修改 更改php.ini文件中的data.tim ...
- 算法导论(第三版) Exercises4.2(求最大和子数组的算法优化过程)
4.1-1 如所有元素都为负,则返回所有元素中最大的负数. 4.1-2(暴力法求最大和子数组) struct subarray { int start, end, sum; }; void brute ...
- MongoDB命令行操作
本文专门介绍MongoDB的命令行操作.其实,这些操作在MongoDB官网提供的Quick Reference上都有,但是英文的,为了方便,这里将其稍微整理下,方便查阅. 这里用来做测试的是远端(10 ...