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],
]

要求

给定两个整数(n,k),返回长度为k,从1到n的所有组合(注意1.组合没有顺序  2. [2,3]和[3,2]为同一组,取前者小于后者的那一组)。

 class Solution {
public:
vector<vector<int> > combine(int n, int k) {
v.resize(k);
build(,n,k,);
return res;
}
void build(int dep, int n, int k,int start){
if(dep==k){
res.push_back(v);
return;
}
for(int i=start;i<=n;i++){
v[dep]=i;
build(dep+,n,k,i+);
} }
vector<int> v;
vector<vector<int>> res;
};

[LeetCode] Combinations——递归的更多相关文章

  1. [LeetCode] Combinations (bfs bad、dfs 递归 accept)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  2. [LeetCode] Combinations 组合项

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  3. LeetCode——Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  4. leetcode — combinations

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...

  5. [LeetCode] Combinations [38]

    称号 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...

  6. [leetcode]Combinations @ Python

    原题地址:https://oj.leetcode.com/problems/combinations/ 题意:组合求解问题. 解题思路:这种求组合的问题,需要使用dfs来解决. 代码: class S ...

  7. [Leetcode] combinations 组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  8. [LeetCode] Combinations 回溯

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  9. 70. Climbing Stairs【leetcode】递归,动态规划,java,算法

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

随机推荐

  1. [bzoj2726][SDOI2012]任务安排 ——斜率优化,动态规划,二分,代价提前计算

    题解 本题的状态很容易设计: f[i] 为到第i个物件的最小代价. 但是方程不容易设计,因为有"后效性" 有两种方法解决: 1)倒过来设计动态规划,典型的,可以设计这样的方程: d ...

  2. css3文件树

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. (转)python 模块安装包 制作

    转自: http://testerhome.com/topics/539 用过python的同学对于python setup.py install肯定不会陌生.那么我们自己如果封装了很多的方法怎么很好 ...

  4. UVA 306 Cipher

    题意 :lucky cat里有翻译.英文也比较好懂. 很容易发现有周期然后就拍就好了 注意每组数据后边都有空行 包括最后一组.一开始以为最后一组没有空行.唉.. #include <map> ...

  5. Delphi栈对象

    来自:http://blog.csdn.net/iseekcode/article/details/5158985 ------------------------------------------ ...

  6. 正则表达式之Regex.Match()用法

    //匹配字符串中的连续数字 string txt = "AAA12345678AAAA"; string m = Regex.Match(txt, @"\d+" ...

  7. C/C++/C#程序如何打成DLL动态库

    C/C++程序如何打成DLL动态库:1.在VS中新建main.h,添加如下内容:extern "C" _declspec(dllexport) int onLoad(); 2.新建 ...

  8. 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)F 猴子排序的期望【Java/高精度/组合数学+概率论】

    链接:https://www.nowcoder.com/acm/contest/116/F 来源:牛客网 题目描述 我们知道有一种神奇的排序方法叫做猴子排序,就是把待排序的数字写在卡片上,然后让猴子把 ...

  9. 洛谷——P1595 信封问题

    P1595 信封问题 题目描述 某人写了n封信和n个信封,如果所有的信都装错了信封.求所有信都装错信封共有多少种不同情况. 输入输出格式 输入格式: 一个信封数n(n<=20) 输出格式: 一个 ...

  10. T-SQL语言基础(转载)

    本文转自http://www.cnblogs.com/Jolinson/p/3552786.html 这里的摘抄来自<Microsoft SQL Server 2008技术内幕:T-SQL语言基 ...