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 ...
随机推荐
- 【Hibernate】无外键多表查询
无外键多表查询时编写hql,直接使用逗号分隔表,where作为联合查询条件进行查询.查询出来的结果可为两种,List<List<Object>>或者List<Map< ...
- 怎样使用windows命令行,用notepad打开某文件夹下面的所有文件
http://zhidao.baidu.com/question/2138815012359999388.html __________________________________________ ...
- C#构架之基础学习----动态添加窗体和 控件
仿照窗体应用程序编写: 任务一:生成一个Form类的窗体对象frm using System.Windows.Forms; //using指令使用Form对象创建所需的命名空间 //如 ...
- ubuntu12.04中如何设定中文输入法
安装 ibus 终端输入:sudo apt-get install ibus ibus-clutter ibus-gtk ibus-gtk3 ibus-qt4 启动 ibus 终端输入 : im-s ...
- docker 学习笔记20:docker守护进程的配置与启动
安装好docker后,需要启动docker守护进程.有多种启动方式. 一.服务的方式 因为docker守护进程被安装成服务.所以,可以通过服务的方式启停docker守护进程,包括查看状态. sudo ...
- 一个RPC的demo
从下面的例子中可以看到,Consumer(client)的代码中引用了Provider部分的class,本例中是 com.provider.EchoServiceImpl和com.provider.E ...
- Hadoop 2.x(YARN)安装配置LZO
今天尝试在Hadoop 2.x(YARN)上安装和配置LZO,遇到了很多坑,网上的资料都是基于Hadoop 1.x的,基本没有对于Hadoop 2.x上应用LZO,我在这边记录整个安装配置过程 1. ...
- csu1306: Manor
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1306 解题思路:唬人的水题,只要按照他的意思打,就能过,不过,数组最好开大点.用到优先队列,也可以 ...
- Aizu 1335 Eequal sum sets
Let us consider sets of positive integers less than or equal to n. Note that all elements of a set a ...
- oracle 11gR2默认密码修改
很久以前装了Oracle,今天终于下决心要学一学了,结果一上午的时间就贡献给如何连接数据库上了 忘记了安装时设置的用户名和密码怎么办?查了下网上的资料,终于解决了! 方法一: 首先进入sqlplus: ...