给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。

示例:

输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]

class Solution {
public:
vector<vector<int> >res;
int N;
int K;
vector<vector<int> > combine(int n, int k)
{
if(n == 0 || k > n)
return res;
K = k;
N= n;
vector<int> v;
DFS(1, v, 0);
return res;
} void DFS(int pos, vector<int> &v, int cnt)
{
if(cnt == K)
{
res.push_back(v);
return;
}
for(int i = pos; i <= N; i++)
{
v.push_back(i);
DFS(i + 1, v, cnt + 1);
v.pop_back();
}
}
};

Leetcode77. Combinations组合的更多相关文章

  1. [FollowUp] Combinations 组合项

    这是Combinations 组合项 的延伸,在这里,我们允许不同的顺序出现,那么新的题目要求如下: Given two integers n and k, return all possible c ...

  2. [LeetCode] Combinations 组合项

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

  3. LeetCode77:Combinations

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

  4. combinations(组合)

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

  5. 【LeetCode每天一题】Combinations(组合)

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

  6. leetCode 77.Combinations (组合)

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

  7. [Leetcode] combinations 组合

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

  8. LeetCode77. Combinations(剑指offer38-2)

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

  9. 077 Combinations 组合

    给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合.例如,如果 n = 4 和 k = 2,组合如下:[  [2,4],  [3,4],  [2,3],  [1,2],  [ ...

随机推荐

  1. git difff

    Generate patch through git diff http://stackoverflow.com/questions/1191282/how-to-see-the-changes-be ...

  2. FreeMarker 自定义 TemplateDirectiveModel(二)

    FreeMarker 是一个用 Java 语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker 与 Web 容器无关,即在 Web 运行时,它并不知道 Servlet 或 HTTP.它不 ...

  3. SpringBoot学习笔记(四):SpringBoot集成Mybatis-Plus+代码生成

    简介 官网:http://baomidou.oschina.io/mybatis-plus-doc/ 平时业务代码不复杂的时候我们写什么代码写的最多,就是我们的SQL语句啊,配置那么多的Mapper. ...

  4. LINK : fatal error LNK1104: 无法打开文件“ucrtd.lib”

    先说解决方案: 选中项目->右键->属性->常规 -->Windows SDK     改成当前系统的SDK版本,我这边是10.0.15063.0,重新生成即可 下载cefsh ...

  5. LOJ10157——皇宫看守(树形DP)

    传送门:QAQQAQ 题意:在一个树上放置守卫,使每一个节点都至少有相邻一节点放置守卫,使最终经费最少 思路:树形DP 首先会想到没有上司的舞会,0表示不放守卫,1表示放守卫,但考虑到对于当前点不放守 ...

  6. PAT甲级——A1074 Reversing Linked List

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  7. JavaScript性能优化篇js优化

    JavaScript性能优化篇js优化   随着Ajax越来越普遍,Ajax引用的规模越来越大,Javascript代码的性能越来越显得重要,我想这就是一个很典型的例子,上面那段代码因为会被频繁使用, ...

  8. Python学习day03 - Python基础(1)

    1. 执行Python程序的两种方式 (1)交互式(Jupyter) 优点:运行一句执行一句 缺点:关闭即消失# (2)命令行式(pycharm) 优点:可以一直保存 缺点:全部写完才能调试bug虽然 ...

  9. Nginx简介与基础配置

    何为Nginx? Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.最初是为了解决C10k的问题,由Igor ...

  10. 分批次删除大表数据的shell脚本

    #!/bin/bash # 分别是主机名,端口,用户,密码,数据库,表名称,字段名称 readonly HOST="XXX" readonly PORT=" readon ...