LeetCode(77):组合
Medium!
题目描述:
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 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):组合的更多相关文章
- Java实现 LeetCode 77 组合
77. 组合 给定两个整数 n 和 k,返回 1 - n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...
- LeetCode 77. 组合(Combinations)
题目描述 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...
- Leetcode之回溯法专题-77. 组合(Combinations)
Leetcode之回溯法专题-77. 组合(Combinations) 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输 ...
- LeetCode:组合总数III【216】
LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- leetcode排列组合相关
目录 78/90子集 39/40组合总和 77组合 46/47全排序,同颜色球不相邻的排序方法 78/90子集 输入: [1,2,2] 78输出: [[], [1], [2], [1 2], [2], ...
- LeetCode 77,组合挑战,你能想出不用递归的解法吗?
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode第46篇文章,我们一起来LeetCode中的77题,Combinations(组合). 这个题目可以说是很精辟了,仅仅 ...
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- [LeetCode] 77. Combinations 全组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- leetCode 77.Combinations (组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
随机推荐
- linux下搭建lamp环境以及安装swoole扩展
linux下搭建lamp环境以及安装swoole扩展 一.CentOS 6.5使用yum快速搭建LAMP环境 准备工作:先更新一下yum源 我安装的环境是:apache2.2.15+mysql5 ...
- Spark SQL自定义外部数据源
1 涉及到的API BaseRelation: In a simple way, we can say it represents the collection of tuples with know ...
- 生成器的throw和close方法
def gen_func(): try: yield 1 except Exception as e: pass yield 2 yield 3 yield 4 yield 5 return &quo ...
- MyBatis中---数据库配置的属性名冲突问题
一.db.properties 属性文件中 最好加特殊的标志前缀 jdbc.username ,如果单纯的username有可能影响到 mapper.xml中的 ${username}; 举例 ...
- 强网杯2018 Web签到
Web签到 比赛链接:http://39.107.33.96:10000 比赛的时候大佬对这题如切菜一般,小白我只能空流泪,通过赛后看别人的wp,我知道了还有这种操作. 这个赛题分为3层 第一层 Th ...
- Css - 字体图标
Css - 字体图标 字体格式 ttf.otf.woff.svg.eot 现在流行将图标做成矢量的字体格式的文档,很多用户在放大页面的时候页面上的普通图片格式的图标就会变得模糊不清,这种字体图标在网页 ...
- js中创建数组,并往数组里添加元素
数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限,是长 ...
- 修复服务器上出现ImportError: cannot import name main的问题
在服务器上成功升级pip2之后再运行pip2命令出现如下报错信息 Traceback (most recent call last): File "/usr/bin/pip2.7" ...
- 20165221 Linux安装及命令入门学习
安装过程 按照图文教程,进行操作,遇到如下问题. 1.安装ubuntu时从官网下载不成功. 最后在同学的帮助下,通过中文版网址入口进入,完成下载. 2.BIOS未恢复出厂设置,导致不能选择64-bit ...
- LaTeX Error: Something's wrong--perhaps a missing \item
使用Latex 引用参考文献,.bib文件是个很好的助手,创建后 1.第一步点击Latex编译,可以获得*.aux文件.*.dvi文件.*.log文件以及*.gz文件: 2.第二步点击Bibtex编译 ...