[Leetcode] combinations 组合
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 组合的更多相关文章
- [LeetCode] Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [FollowUp] Combinations 组合项
这是Combinations 组合项 的延伸,在这里,我们允许不同的顺序出现,那么新的题目要求如下: Given two integers n and k, return all possible c ...
- LeetCode:组合总数III【216】
LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- 【LeetCode每天一题】Combinations(组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- 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. Example: I ...
- LeetCode 77. 组合(Combinations)
题目描述 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...
- LeetCode——Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
随机推荐
- dedesmc 手机端生成静态页
dedesmc 手机端生成静态页 1.首先下载插件,下载地址:https://pan.baidu.com/s/1Nfx_KBYuxRkZ7VzoPxy28g 密码:83x7 2.进入 dedecms ...
- 移植Linux Kernel SM750 驱动到VxWorks 7
一.SM750简介 SM750 是SiliconMotion 推出的一款适合嵌入式设备的显卡(Embedded GPU),采用PCIe接口与CPU连接,内部集成16MB DDR SDRAM显存,产品具 ...
- stm32f103 time2配置,转载
//----------------------------main()-------------------- //stm32f103c8t6有3个普通1个高级定时器 //每次进入中断服务程序间隔时 ...
- ScriptManager和UpdatePanel用法 (ajax)
ScriptManager和UpdatePanel控件联合使用可以实现页面异步局部更新的效果.其中的UpdatePanel就是设置页面中异 步局部更新区域,它必须依赖于ScriptManager存在, ...
- PATA1034题解
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 参考:算法笔记(胡凡)10.3.1 # ...
- ORA-12705: Cannot access NLS data files or invalid
RedHat7.1 Oracle11gr2 oracle 默认的编码方式如下:SQL> select userenv('language') from dual; USERENV('LANGUA ...
- Linux上jdk的安装(CentOS6.5)
centos openjdk 安装 http://www.cnblogs.com/ilahsa/archive/2012/12/11/2813059.html 知CentOS6.5桌面版默认安装的是J ...
- 「日常训练」「小专题·图论」 Cow Contest (1-3)
题意 分析 问题是要看出来这是个floyd闭包问题.我没看出来- - 分析之后补充. 代码 // Origin: // Theme: Graph Theory (Basic) // Date: 080 ...
- 【赛后补题】(HDU6228) Tree {2017-ACM/ICPC Shenyang Onsite}
这条题目当时卡了我们半天,于是成功打铁--今天回来一看,mmp,贪心思想怎么这么弱智.....(怪不得场上那么多人A了 题意分析 这里是原题: Tree Time Limit: 2000/1000 M ...
- sphinx调用API参考(官方手册)
API的参考实现是用PHP写成的,因为(我们相信)较之其他语言,Sphinx在PHP中应用最广泛.因此这份参考文档基于PHP API的参考,而且这节中的所有的代码样例都用PHP给出. 当然,其他所有A ...