给定两个整数 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. 基于知识图谱的APT组织追踪治理

    高级持续性威胁(APT)正日益成为针对政府和企业重要资产的不可忽视的网络空间重大威胁.由于APT攻击往往具有明确的攻击意图,并且其攻击手段具备极高的隐蔽性和潜伏性,传统的网络检测手段通常无法有效对其进 ...

  2. 杂项-SpringBoot-Jasypt:Jasypt(安全框架)

    ylbtech-杂项-SpringBoot-Jasypt:Jasypt(安全框架) 1. 使用jasypt加密Spring Boot应用中的敏感配置返回顶部 1. 本文讲述了在Spring Boot/ ...

  3. 基于neighborhood models(item-based) 的个性化推荐系统

    文章主要介绍的是koren 08年发的论文[1],  2.2neighborhood models部分内容(其余部分会陆续补充上来). koren论文中用到netflix 数据集, 过于大, 在普通的 ...

  4. jmeter命令行压测

    简介:使用非GUI模式,即命令行模式运行jmeter测试脚本能够大大缩减系统资源 1.配置jdk及添加环境变量 变量名:JAVA_HOME 变量值: C:\Program Files\Java\jdk ...

  5. 08-background详解

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

  6. 使用 MongoDB shell访问MongoDB

  7. 【心无旁骛】vue-ts-daily

    这是一个非常有意思的项目,我们先来看看效果 这个项目所用的技术也比较有意思,它的技术栈为vue2.5 + Typescript + vuex + vue-router 放下博主的项目地址吧,https ...

  8. Python ——tempfile

    主要有以下几个函数: tempfile.TemporaryFile 如何你的应用程序需要一个临时文件来存储数据,但不需要同其他程序共享,那么用TemporaryFile函数创建临时文件是最好的选择.其 ...

  9. Delphi 设计模式:《HeadFirst设计模式》Delphi代码---工厂模式之抽象工厂[转]

     1  2 {<HeadFirst设计模式>工厂模式之抽象工厂 }  3 { 抽象工厂的产品                       }  4 { 编译工具:Delphi7.0     ...

  10. Java学习记录--ModelMapper的使用

    在项目中很多时候需要把Model和DTO两个模型类来回转换,保证Model对外是隐私的,同时类似密码之类的属性也能很好地避免暴露在外了. 那么ModelMapper就是为了方便转换而实现的一个类库,下 ...