LeetCode77: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],
]
Hide Tags Backtracking
给定一个数n。求1到n之间的全部的k个数的组合。
这个题目能够在纸上画下草图,明显能够用递归求解。递归的终止条件是k=0,而且因为须要将组合保存到集合vector中,还须要使用回溯法来保存数据。
runtime:8ms
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> result;
vector<int> vec;
helper(1,n,k,vec,result);
return result;
}
void helper(int first,int last,int k,vector<int> & vec,vector<vector<int>> & result)
{
if(k==0)
{
result.push_back(vec);
return ;
}
for(int i=first;i<=last-k+1;i++)
{
vec.push_back(i);
helper(i+1,last,k-1,vec,result);
vec.pop_back();
}
}
};
LeetCode77:Combinations的更多相关文章
- LeetCode77. Combinations(剑指offer38-2)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- Leetcode77. Combinations组合
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3] ...
- [Swift]LeetCode77. 组合 | Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [LeetCode] Factor Combinations 因子组合
Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...
- [LeetCode] Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- Leetcode 254. Factor Combinations
Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...
- 17. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
随机推荐
- 如何使用notepad++搭配MinGW配置编译C/C++
最经开始学C++了,平时学习不喜欢用IDE,一直以来都喜欢使用sublimetext写代码.所以在网上找了一下如何配置sublimetext编译C/C++.不过简单配置之后,只有输出,要想进行输出操作 ...
- Android 中 ListView 常用属性合集
class ListView.FixedViewInfo//用来在列表内展现一个固定位置视图,如在列表顶端的header和在列表底端的footer 一.XML属性 1.ListView的XML属性 a ...
- ios7禁止默认划动返回
self.navigationController.interactivePopGestureRecognizer.enabled = NO; 或 在使用之前先要判断是否ios7,不然会导致crash ...
- 设置QPushButton的平面与突出(遍历控件)
#include "ui_maindialog.h" #include "maindialog.h" #include <QState> #incl ...
- yii_wiki_145_yii-cjuidialog-for-create-new-model (通过CJuiDialog来创建新的Model)
/**** CJuiDialog for create new model http://www.yiiframework.com/wiki/145/cjuidialog-for-create-new ...
- 17.1.1 How to Set Up Replication
17.1.1 How to Set Up Replication 17.1.1.1 Setting the Replication Master Configuration 17.1.1.2 Sett ...
- 动态规划——最长公共子序列(LCS)
/** * @brief longest common subsequence(LCS) * @author An * @data 2013.8.26 **/ #include <iostrea ...
- shu_1548 悟空问题(大哥,主妖怪抓走的朋友!)
http://202.121.199.212/JudgeOnline/problem.php?cid=1078&pid=17 分析: 直接暴力了.. . 代码: #include <s ...
- 梳理一下重装sql2008R2sp1步骤
我的电脑是这样,最早的时候装的是2005,后来公司用到2008,我就手动卸载,但是好像卸载的不够彻底,在装2008的时候,选择升级方式安装. 虽然成功了,但是在运行select @@version 时 ...
- c++构造析构顺序
class A { public: A(){ cout << "constrcut A" << endl; }; ~A(){ cout << & ...