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 ...
随机推荐
- 【JQuery Plugin】WdatePicker
<div class="timeSelect reportDate"> <span>查询时间:</span> <input type=&q ...
- 网络编程TCP协议-聊天室
网络编程TCP协议-聊天室(客户端与服务端的交互); <span style="font-size:18px;">1.客户端发数据到服务端.</span> ...
- nice Validator参考
快速上手 例1. DOM传参 1. 要验证一个表单,只需要给字段绑定规则“data-rule”就可以了2. 字段可以有多条规则,规则之间用分号(;)分隔3. js初始化不是必要的,只要是字段并且带有“ ...
- MessageBox不能前置显示的问题
在MFC的开发中,经常会遇到一些莫名奇妙的问题,可能是经验不足的原因吧. 进入正题....在手头的项目中,用MFC做的界面应用.在某一天突然发现程序界面不能进行响应,经过反复的调试后发现:Messag ...
- 用 Python 测试框架简化测试
用 Python 测试框架简化测试 摘要:本文将向您介绍了三种流行 Python 测试框架(zope.testing,py.test,nose)的基本特性,并讨论新一代的测试风格. 最近出现了行业级的 ...
- 基于visual Studio2013解决C语言竞赛题之0607strcpy
题目
- 疯牛-- Aggressive cows (二分)
疯牛 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小 ...
- Python学习-使用matplotlib画动态多图
最近常常使用matplotlib进行数学函数图的绘制,可是怎样使用matplotlib绘制动态图,以及绘制动态多图.直到今天才学会. 1.參考文字 首先感谢几篇文字的作者.帮我学会了怎样绘制.大家也能 ...
- sql基础,必须会的————随便整理、杂乱无章
1.sqlserver2008r2的安装 2.数据库与表的建立.增加.删除.修改. 3,索引的概念,包括聚集与非聚集的区别.全文索引的建立与如何使用全文索引. 4,重新生成索引,重新组织索引. 5,建 ...
- Eclipse生成Jar包方法
Eclipse生成jar包 第一:普通类导出jar包,我说的普通类就是指此类包含main方法,并且没有用到别的jar包. 1.在eclipse中选择你要导出的类或者package,右击,选择Exp ...