题目描述:

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

示例:

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

要完成的函数:

vector<vector<int>> combine(int n, int k)

说明:

1、这道题给定两个int型整数n和k,要求在[1,n]的闭区间中找到k个整数的组合(组合也就是不重复的)。

比如n=4,k=2,那么所有的组合是 [1,2] [1,3] [1 4] [2,3] [2,4] [3,4]。

把每一个组合放在一个一维vector中,所有的组合存储在二维的vector中,最后返回这个二维的vector。

2、这道题目也是一道递归的题目,我们依然用熟悉的方法来做。

代码如下(附详解):

    vector<vector<int>>res;//全局变量,最终要返回的二维vector
vector<int>res1;//全局变量,存储每一个可能的组合
void digui(int n,int k,int index)
{
if(k==0)//退出条件,当前没有数要加入了
{
res.push_back(res1);
return;
}
for(int i=index;i<=n-k+1;i++)//不断地循环,注意i的范围,比如n=4,k=2的时候,i最大也只能取3,不能取4
{
res1.push_back(i);
digui(n,k-1,i+1);//k减1,少了一个要添加的数,i加1,从下一个坐标开始
res1.pop_back();
}
}
vector<vector<int>> combine(int n, int k)
{
digui(n,k,1);//直接进入递归函数,k表示当前还有几个数要加入,1表示当前要从1开始
return res;//返回最终的二维vector
}

上述代码实测56ms,beats 99.55% of cpp submissions。

leetcode-77-组合的更多相关文章

  1. Java实现 LeetCode 77 组合

    77. 组合 给定两个整数 n 和 k,返回 1 - n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...

  2. LeetCode 77. 组合(Combinations)

    题目描述 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...

  3. Leetcode之回溯法专题-77. 组合(Combinations)

    Leetcode之回溯法专题-77. 组合(Combinations)   给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输 ...

  4. LeetCode:组合总数III【216】

    LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. ...

  5. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  6. leetcode排列组合相关

    目录 78/90子集 39/40组合总和 77组合 46/47全排序,同颜色球不相邻的排序方法 78/90子集 输入: [1,2,2] 78输出: [[], [1], [2], [1 2], [2], ...

  7. LeetCode 77,组合挑战,你能想出不用递归的解法吗?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode第46篇文章,我们一起来LeetCode中的77题,Combinations(组合). 这个题目可以说是很精辟了,仅仅 ...

  8. LeetCode 77 Combinations(排列组合)

    题目链接:https://leetcode.com/problems/combinations/#/description    Problem:给两个正数分别为n和k,求出从1,2.......n这 ...

  9. [LeetCode] 77. Combinations 全组合

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

  10. leetCode 77.Combinations (组合)

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

随机推荐

  1. 解题报告-908. Smallest Range I

    题目 : Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K ...

  2. FIX protocol tutorial : Fix Session is not connecting how to diagnose it ?

    In this blog post of FIX protocol tutorial series I would like to share my experience with connectiv ...

  3. Zookeeper 源码(四)Zookeeper 服务端源码

    Zookeeper 源码(四)Zookeeper 服务端源码 Zookeeper 服务端的启动入口为 QuorumPeerMain public static void main(String[] a ...

  4. DB2存储过程通过游标实现批量数据处理

    CREATE procedure proc_change()LANGUAGE SQLBEGIN DECLARE l_id INTEGER; DECLARE l_detail_id INTEGER; D ...

  5. pyspider示例代码三:用PyQuery解析页面数据

    本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一些 ...

  6. Android代码实现求和运算

    Android代码实现求和运算 实验要求: 用Android语言设计一个界面,点击某按钮实现求和运算. 代码实现 码云链接 核心代码 以上为求和按钮的代码截图,解析如图标注. 实验结果 当输入为空值时 ...

  7. 搭建一个基于CentOS的可视化zookeeper管理工具zkUI实现对zk的可视化管理

    一. zookeeper 可视化工具   JMX => CLRProfile ZKUI => java写的一个可视化的web网站 github中下载 https://github.com/ ...

  8. Postgresql fillfactor

    一个表的填充因子(fillfactor)是一个介于 10 和 100 之间的百分数.100(完全填充)是默认值.如果指定了较小的填充因子,INSERT 操作仅按照填充因子指定的百分率填充表页.每个页上 ...

  9. sweetalert 快速显示两个提示, 第二个显示不出的问题

    今天在使用 sweetalert 做提示框的时候, 有个操作快速做了两次提示, 发现第二次显示不出: sweetAlert({}, function() { $.get('', function() ...

  10. asp.net——上传图片生成缩略图

    上传图片生成缩略图,原图和缩略图地址一样的时候缩略图会把原图覆盖掉 /// <summary> /// 生成缩略图 /// </summary> /// <param n ...