给定两个整数 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. Cordova 常用命令及插件

    安装 cordova: npm install -g cordova 创建应用程序 cordova create hello com.example.hello HelloWorld  添加平台 co ...

  2. Consul 安装的与启动

    1.下载地址:https://www.consul.io/downloads.html linux 下载地址: wget https://releases.hashicorp.com/consul/0 ...

  3. python3-常用模块之time

    import time time模块主要是处理各种类型的时间 常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行,单位为秒. 2.time.time() 获取当前时间戳 时间戳 ...

  4. gulpfile.js demo

    var config = require("./build.config") //获取build.config.js文件里的内容 var gulp = require(" ...

  5. mysql TIMESTAMP 不能为NULL

    一般建表时候,创建时间用datetime,更新时间用timestamp.这是非常重要的. 我测试了一下,如果你的表中有两个timestamp字段,只要你更新任何非timestamp字段的值,则第一个t ...

  6. ckeditor图片上传二三事

    最近实验室要用ckeditor,踩了几个小坑记录下. 1.出现iframe跨域问题 response.setHeader("X-Frame-Options", "SAME ...

  7. highchart接收后台数据用法

    最近做数据分析的时候使用了highchart这个插件,从后台中接收数据的时候出了一些问题,记录下来免得以后忘了. $(function () { var list = {$weeklist}; var ...

  8. 常用es6特性归纳-(一般用这些就够了)

    之前做vue和react的时候,发现文档什么的最新版本都建议用es6写法,对es6友好度更高,加之现在es6也越来越普及,兼容性问题直接用babel转码就好了,特别方便,于是我开始学着用es6写代码, ...

  9. Ionic 图片预览ion-slide-box,ion-slide,ion-scroll实现

    1.index.html 代码 <body ng-app="starter"> <ion-pane> <ion-header-bar class=&q ...

  10. html特殊字符 编码css3 content:"特殊符号"一览

    工作中经常会用到用纯css3美化复选框 <div class="cross"></div> css代码.cross{ width: 20px; height ...