[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 ...
随机推荐
- php-5.6.26源代码 - hash存储结构 - 添加
添加 , (void *)module, sizeof(zend_module_entry), (void**)&module_ptr){ // zend_hash_add 定义在文件“php ...
- python练习笔记
python练习笔记,装饰器.定制方法生成特定的类 # -*- coding: utf-8 -*- def catch_exception(func): def wrap(self, *args, * ...
- 使用SQLite删除Mac OS X 中launchpad里的快捷方式
一般情况下,从App Store安装的应用程序,如果应用删除,那么launchpad里对应的图标会一起删除了. 而对于不是通过App Store安装的应用程序,删除应用程序,Launchpad中很可能 ...
- Java学习笔记六:Java的流程控制语句之if语句
Java的流程控制语句之if语句 一:Java条件语句之if: 我们经常需要先做判断,然后才决定是否要做某件事情.例如,如果考试成绩大于 90 分,则奖励一朵小红花 .对于这种“需要先判断条件,条件满 ...
- STL——泛型编程
1.指针的算术运算 对于一个存储int数据的vector,我们要查找某值是否存在于其中,采用下标操作的做法如下: int* find(const vector<int> &vec, ...
- 一起来学习Shell脚本
Shell脚本 Shell脚本(shell script),是一种为shell编写的脚本程序. 大家所说的shell通常都是指的shell脚本,但其实shell与shell脚本是两个不同的概念.由于习 ...
- OVERLAY(文字の上書き)
OVERLAY 命令により.文字列が別の文字列によって上書きされます. OVERLAY c1 WITH c2 [ONLY str]. この命令により.項目 c1 のすべての位置のうち.str の中に出 ...
- python2.7练习小例子(十三)
13):题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5. 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成.(1)如果这个质数恰等于 ...
- gp更新来的太快
意外总是会发生 添加一个判断function的分支,过滤掉function,其实也考虑再进一步去分析它的作用,稍后再议. 更新一下 var gnp = { get: function(url) { r ...
- java二分法来求一个数组中一个值的key
package TestArray; import java.util.Arrays; /** * 二分法查找 */ public class Test { public static void ma ...