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的更多相关文章

随机推荐

  1. OOP(面向对象编程)的一些特性

    接口:接口是把公共实例(非静态)方法和属性结合起来,以封装特定功能的一个集合.一旦定义了接口,就可以在类中实现它.接口注意事项:接口不能单独存在.不能像实例化一个类那样实例化接口.另外,接口不能包含实 ...

  2. STARTUP.A51详解及如何使能可重入函数

    $NOMOD51       ;Ax51宏汇编器控制命令:禁止预定义的8051;------------------------------------------------------------ ...

  3. 64位调试器花费的时间比预期的要长(A 64-bit debugging operation is taking longer than expected)

    在stackoverflow上找到解决方案的: http://stackoverflow.com/questions/21329899/vs2013-professional-local-64-bit ...

  4. codility上的问题 (23)Chi 2012

    这个题也比较有意思.意思是给定一个数组A,长度为M,里面都是正整数,代表每块地形的高度.现在要测试一种加农炮,给定一个炮弹的高度H, 如果存在最小的I,满足0 < I <  M,满足A[I ...

  5. 黑马程序员_Java面向对象_包

    7.包 7.1包(package) 对类文件进行分类管理. 给类提供多层命名空间. 写在程序文件的第一行. 类名的全称是:包名.类名. 包也是一种封装形式. 利用命令行自动生成文件夹格式:D:\jav ...

  6. hdu 2254 奥运

    点击打开hdu 2254 思路: 矩阵乘法 分析: 1 题目给定一个有向图,要求t1-t2天内v1-v2的路径的个数 2 根据离散数学里面的可达矩阵的性质,我们知道一个有向图的邻接矩阵的前n次幂的和即 ...

  7. myeclipse实现Servlet实例(1) 通过继承servlet接口实现

    1.在myeclipse新建web project,配置Tomcat(在myeclipse的Window--preferences) 2.然后在src新建servlet文件( 此处放在com.tsin ...

  8. JAVA模拟表单提交

    这是我网上搜的,自己使用也蛮方便,所以上传供大家分享. package wzh.Http;   import java.io.BufferedReader; import java.io.IOExce ...

  9. roleManager 元素(ASP.NET 设置架构),我是因为SSL弱密码(转)

    为角色管理配置应用程序. 此元素是 .NET Framework 2.0 版中的新元素. configuration 元素(常规设置架构)  system.web 元素(ASP.NET 设置架构)   ...

  10. UIImageView填充模式(contentMode)

    UIViewContentModeScaleToFill :拉伸填充UIImageView满屏          UIViewContentModeScaleAspectFit :拉伸到合适UIIma ...