Medium!

题目描述:

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

示例:

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

解题思路:

这道题让求1到n共n个数字里k个数的组合数的所有情况,还是要用深度优先搜索DFS来解,根据以往的经验,像这种要求出所有结果的集合,一般都是用DFS调用递归来解。那么我们建立一个保存最终结果的大集合res,还要定义一个保存每一个组合的小集合out,每次放一个数到out里,如果out里数个数到了k个,则把out保存到最终结果中,否则在下一层中继续调用递归。https://blog.csdn.net/u010500263/article/details/18435495中有一张图很好的说明了递归调用的顺序。

C++解法一:

 class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> res;
vector<int> out;
helper(n, k, , out, res);
return res;
}
void helper(int n, int k, int level, vector<int>& out, vector<vector<int>>& res) {
if (out.size() == k) res.push_back(out);
for (int i = level; i <= n; ++i) {
out.push_back(i);
helper(n, k, i + , out, res);
out.pop_back();
}
}
};

对于n = 5, k = 3, 处理的结果如下:

1 2 3 
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5

我们再来看一种迭代的写法,也是一种比较巧妙的方法。这里每次先递增最右边的数字,存入结果res中,当右边的数字超过了n,则增加其左边的数字,然后将当前数组赋值为左边的数字,再逐个递增,直到最左边的数字也超过了n,停止循环。对于n=4, k=2时,遍历的顺序如下所示:

0 0 #initialization
1 0
1 1 #push_back
1 2 #push_back
1 3 #push_back
1 4 #push_back
1 5
2 5
2 2 #push_back
2 3 #push_back
2 4 #push_back
...
3 4 #push_back
3 5
4 5
4 4
4 5
5 5 #stop

C++解法二:

 class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> res;
vector<int> out(k, );
int i = ;
while (i >= ) {
++out[i];
if (out[i] > n) --i;
else if (i == k - ) res.push_back(out);
else {
++i;
out[i] = out[i - ];
}
}
return res;
}
};

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. LOJ #2026「JLOI / SHOI2016」成绩比较

    很好的锻炼推柿子能力的题目 LOJ #2026 题意 有$n$个人$ m$门学科,第$ i$门的分数为不大于$U_i$的一个正整数 定义A「打爆」B当且仅当A的每门学科的分数都不低于B的该门学科的分数 ...

  2. java并发基础

    一.创建线程 1.使用Executor:java.util.concurrent包中的执行器(Execute)为你管理Thread对象,从而简化并发编程. package thread; import ...

  3. Service生命周期

    https://www.cnblogs.com/huihuizhang/p/7623760.html (1)Service是单例的,只要没有destroy,多次startService或bindSer ...

  4. 解码base64加密的图片并打印到前台

    经过base64加密的图片对象是一串字符串,我们解码后可通过流将其打印出来: utils类: package com.aebiz.app.web.modules.controllers.open.ap ...

  5. 类ThreadLocal的使用与源码分析

    变量值的共享可以使用public static的形式,所有的线程都使用同一个变量.如果每个线程都有自己的共享变量,就可以使用ThreadLocal.比如Hibernat的session问题就是存在Th ...

  6. Python 面向对象【2】

    组合 [适合没干系的横向类,比如池塘,乌龟,鱼]  把类的实例化放到新类里面     属性名和方法名相同,属性会覆盖掉方法   绑定 方法需要实例才能被调用!! 这种限制即绑定 用self接受绑定   ...

  7. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  8. asyncio Queue的使用例子

    import aiohttp import asyncio import async_timeout from urllib.parse import urljoin, urldefrag root_ ...

  9. 【转】Java并发编程:如何创建线程?

    一.Java中关于应用程序和进程相关的概念 在Java中,一个应用程序对应着一个JVM实例(也有地方称为JVM进程),一般来说名字默认是java.exe或者javaw.exe(windows下可以通过 ...

  10. exiting pxe rom 无法启动

    背景 我这是给人装了多少次机器了,上千次不敢说,几百次是有了.有个奇怪现象,为什么每次总有新的问题呢,极少能一次成功的.除了让我涨了见识,没想到其他的用处.程序员修电脑,搞笑吧,还有找我修洗衣机的,说 ...