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以内的正整数,每个数有放入与不放入两种选择=>带回溯的递归

class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> result;
vector<int> pre;
dfs(result,pre,n,k,);
return result;
} void dfs( vector<vector<int>> &result, vector<int> pre, int n, int k, int depth)
{
if(depth > n) return;
pre.push_back(depth); if(pre.size() < k)
{
dfs(result,pre,n,k,depth+);
}
else
{
result.push_back(pre);
}
pre.pop_back();
dfs(result,pre,n,k,depth+);
}
};

77. Combinations (Recursion)的更多相关文章

  1. [LeetCode] 77. Combinations 全组合

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

  2. Leetcode 77, Combinations

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

  3. 77. Combinations

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...

  4. 77. Combinations(medium, backtrack, 重要, 弄了1小时)

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

  5. 【一天一道LeetCode】#77. Combinations

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  6. [leetcode]77. Combinations组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

  7. 77. Combinations (java 求C(n,k)的组合,排除重复元素)

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 解析:同求全 ...

  8. (效率低下)77. Combinations C++回溯法 组合

    https://leetcode.com/problems/combinations/ 沿用78题的思路 class Solution { public: void backTrack(vector& ...

  9. LeetCode OJ 77. Combinations

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

随机推荐

  1. c++的c风格字符串函数的实现

    要注意使用断言判断传入的字符串非空. #include <cassert> //求字符串长度 size_t StrLen(const char *str) { assert(str != ...

  2. zepto 自定义build

    为啥要自定义build? 一般来说我们都会直接在官网下,但它只包含默认几个的模块,对于移动开发,这些模块有些是需要的,有些则可以不用.所以我们可以根据自己的需要来定制. 下图为zepto包含的模块,其 ...

  3. MySQL 主从复制与读写分离

    1.MySQL主从复制入门 首先,我们看一个图: 影响MySQL-A数据库的操作,在数据库执行后,都会写入本地的日志系统A中. 假设,实时的将变化了的日志系统中的数据库事件操作,在MYSQL-A的33 ...

  4. 《DSP using MATLAB》Problem 2.9

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  5. 3.Appium运行时出现:Original error: Android devices must be of API level 17 or higher. Please change your device to Selendroid or upgrade Android on your device

    参考博客:https://blog.csdn.net/niubitianping/article/details/52624417 1.错误信息:Original error: Android dev ...

  6. Appcan、apicloud、HBuilder 不同之处解析

    来源:http://www.mamicode.com/info-detail-1129829.html 现在Hybrid app是一中非常火热的开发模式,在国内对应的开发工具也乱象丛生,有WeX5.c ...

  7. 用Linq对String类型的数字求和

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. Codeforces Round #474-E(树形dp)

    一.题目链接 http://codeforces.com/contest/960/problem/B 二.题意 给定一棵$N$个节点的树,每个节点的权值$V$.定义树中两点$u_1$和$u_m$的权值 ...

  9. Clearsigned file isn't valid, got 'NOSPLIT' (does the network require authentication?)

    ubuntu16在运行sudo apt-get update 命令后,报出错误: Clearsigned file isn't valid, got 'NOSPLIT' (does the netwo ...

  10. 停止调试 IIS 不退出

    在VS主面板打开:工具->选项->调试->编辑继续   取消选中[启用"编辑并继续"] 就可以(不过这是针对所有的调试). 若只想对单个项目进行设置,可以选择自己 ...