LeetCode_Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,
If n = 4 and k = 2, a solution is: [
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
class Solution {
private:
vector<vector<int>> result;
vector<int> a ;
public:
void findResult(int n, int k, int num,int start )
{
if(num == k)
{
result.push_back(a);
return;
}
for(int i = start; i <= n; i++)
{
a[num] = i;
findResult( n, k, num+, i+);
}
}
vector<vector<int> > combine(int n, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
a.resize(k);
result.clear();
findResult( n, k, , ) ;
return result;
}
};
重写后:
//Combinations
class Solution {
public:
void DFS(int n, int start,int k, vector<int> &ans){
if(ans.size() == k){
res.push_back(ans);
return;
}
for(int i = start; i <= n; i++)
{
ans.push_back(i);
DFS(n, i+, k, ans);
ans.pop_back();
}
}
vector<vector<int> > combine(int n, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
res.clear();
vector<int> ans;
DFS(n, , k, ans);
return res; }
private:
vector<vector<int>> res;
};
LeetCode_Combinations的更多相关文章
随机推荐
- 工厂方法模式 - OK
工厂方法模式(Factory Method),定义了一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使一个类的实例化延迟到子类. 工厂方法模式在实现时,客户端需要决定实例化哪一个工厂来实现运 ...
- PowerShell中调用外部程序和进程操作命令例子
学习PowerShell,我们不指望通过C#编程去搞定所有事情,我们应该记住cmd.exe或者说批处理给我们留下的宝贵财富——通过调用外部程序去解决问题.调用了外部程序,势必就要对进程进行管理,这就是 ...
- windows driver inf文件
原来修改了inf文件会导致签名过的驱动包哈希值不正确了啊.现在才知道. = = http://www.chiphell.com/thread-827956-1-1.html
- Manage Spring Boot Logs with Elasticsearch, Logstash and Kibana
下载地址:https://www.elastic.co/downloads When time comes to deploy a new project, one often overlooked ...
- 【转】Android Service完全解析,关于服务你所需知道的一切(下) ---- 不错
原文网址:http://blog.csdn.net/guolin_blog/article/details/9797169 转载请注册出处:http://blog.csdn.net/guolin_bl ...
- 【转】ubuntu14.04 trusty的源
原文网址:http://blog.chinaunix.net/uid-15041-id-4821715.html 一.编辑更新源文件:/etc/apt/sources.list二.更新源索引文件:ap ...
- 关于as3实现对任何对象进行深刻复制的思考
无意中看到关于as3深度复制思考的文章,觉得不错,于是转来记录以后用到可以参考. 转载来源:http://xmchcly.iteye.com/blog/1307425,下面是转文: 通过 ByteAr ...
- (Stack)Basic Calculator I && II
Basic Calculator I Implement a basic calculator to evaluate a simple expression string. The expressi ...
- [教程]隐藏ActionBar中的MenuItem
有时候我们需要在不同的时候改变ActionBar中MenuItem的项数,或者隐藏某些MenuItem,百度上找了很久没什好资料,还是Google了一下,StackOverFlow上有大神解决了. 先 ...
- phpcms:三、头部包含
1‘标题:{if isset($SEO['title']) && !empty($SEO['title'])}{$SEO['title']}{/if}{$SEO['site_title ...