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

For example,
If n = 4 and k = 2, a solution is:

[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]

题意:给定1...n个数,求出每k个数的组合情况。

思路:使用DFS。定义中间数组变量,每当其大小为k时,将其存入结果res;若不等于则继续调用。需要注意的是,组合是不讲究顺序的,所以下层的递归不用从头开始,只需从当前的下一个数字开始就行,另外,对第一层的选取,使用迭代,这样就可以考虑到所有情况,后面的从下层开始,用递归就好。这里有详细的解释。代码如下:

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

注:排列和组合的区别在于和顺序有关,如:[1,2]、[2,1]是两种不同的排列,却是相同的组合。

[Leetcode] combinations 组合的更多相关文章

  1. [LeetCode] Combinations 组合项

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

  2. [FollowUp] Combinations 组合项

    这是Combinations 组合项 的延伸,在这里,我们允许不同的顺序出现,那么新的题目要求如下: Given two integers n and k, return all possible c ...

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

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

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

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

  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]77. Combinations组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

  8. LeetCode 77. 组合(Combinations)

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

  9. LeetCode——Combinations

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

随机推荐

  1. Apache httpd Server 配置正向代理

    背景 代理(Proxy),位于客户端与实际服务端之间,当客户端需要请求服务端内容时,先向代理发起请求,代理将请求转发到实际的服务器,再原路返回.也可以在代理服务器设置缓存,将实际服务器上不常变化的内容 ...

  2. HTTP-Http状态码301和302的区别

    官方解释: 301 redirect: 301 代表永久性转移(Permanently Moved) 302 redirect: 302 代表暂时性转移(Temporarily Moved ) 共同点 ...

  3. php xml转数组 自定义xml_to_array

    <?php header("Content-type: text/xml; charset=utf-8"); $con = file_get_contents('xml路径' ...

  4. ruby File类

    类方法 路径相关: File.basename(filename <, suffix>) -> string返回给定文件名 filename 的最后一部分.如果有 suffix 参数 ...

  5. 小程序开发-7-访问api数据与ES6在小程序中的应用

    访问API数据与ES6在小程序中的应用 看待组件的两种观点 组件复用 代码分离-(特别重要) 不能在一个页面写所有的代码,代码分离具有很强的可读性.可维护性 Blink Api 介绍与测试API ur ...

  6. Waterline从概念到实操

    Waterline基本介绍 Waterline是什么 Waterline是下一代存储和检索引擎,也是Sails框架中使用的默认ORM . ORM的基本概念 Object Relational Mapp ...

  7. spring boot打包问题

    java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories ...

  8. 高德API+.NET解决租房问题(新增诚信房源)

    作者:李国宝链接:https://zhuanlan.zhihu.com/p/22105008(欢迎点赞)来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 之前有小伙伴反应 ...

  9. FPGA数字鉴相鉴频器的开发记录

    1. 对于电机的锁相控制,需要对相差进行PI性质的环路滤波,但现有的锁相环中鉴频鉴相器输出为相差脉冲而非数字量,难以直接进行PI特性的环路滤波. 通过对晶振的非整数分频获取准确的参考时钟,基于触发器机 ...

  10. Jmeter非GUI命令参数说明

    查看帮助 -h, --help print usage information and exit 查看版本 -v, --version print the version information an ...