LeetCode OJ 77. 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],
]
解答
用递归,对i-n求k个数的组合,先确定一个数j(其中i <= j <= n + 1 - k),然后对(i + 1)-n求(k - 1)个数的组合,递归终止的条件是在一个数列中求1个数的组合,也就是将这个数列枚举一遍。
下面是AC的代码:
class Solution {
public:
vector<vector<int>> generate_combinations(int start, int n, int k){
vector<vector<int>> ret, temp;
if(k == 1){
for(int i = start; i <= n; i++){
vector<int> combination;
combination.push_back(i);
ret.push_back(combination);
}
return ret;
}
for(int i = start; i + k <= n + 1; i++){
temp = generate_combinations(i + 1, n, k - 1);
for(vector<vector<int>>::iterator iter = temp.begin(); iter != temp.end(); iter++){
iter->insert(iter->begin(), i);
ret.push_back(*iter);
}
}
return ret;
}
vector<vector<int>> combine(int n, int k) {
return generate_combinations(1, n, k);
}
};
116
LeetCode OJ 77. Combinations的更多相关文章
- 【LeetCode】77. Combinations 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【一天一道LeetCode】#77. Combinations
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【LeetCode】77. Combinations (2 solutions)
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...
- LeetCode OJ:Combinations (排列组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- [LeetCode] 77. Combinations 全组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- Leetcode OJ 刷题
Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
随机推荐
- U3D学习06-数学基础
1.fixed timestep 固定帧率, 2.time scale 快慢镜头,影响的是真实时间 3.time.deltatime增量时间,物体运动不受帧频率影响,每秒移动速度需要乘deltatim ...
- 第15课 右值引用(2)_std::move和移动语义
1. std::move (1)std::move的原型 template<typename T> typename remove_reference<T>::type& ...
- hive之权限问题AccessControlException Permission denied: user=root, access=WR
问题描述:在集群上,用hive分析数据出现如下错误 FAILED: Execution Error, return code from org.apache.hadoop.hive.ql.exec.D ...
- Windows 2016 无域故障转移群集部署方法 超详细图文教程 (二)
上一章我们配置了一台设备,接着根据那个配置,配置其它设备.这里我配置了三台设备: 创建故障转移群集,并添加设备. 之前的操作都是每台服务器都要做的,而这个操作,只需要任选一台去做即可,我这里选d1 1 ...
- (转)C# 控制蜂鸣器发声
原文地址:http://blog.csdn.net/tsinfeng/article/details/6201918 在C#中可以通过以下四种方式来实现蜂鸣或者报警,播放声音之类的功能.XP下对蜂鸣有 ...
- TP微信扫码支付
1.官网下载php扫码支付adk,放在项目引入第三方类库中 2.配置config中相关参数 注意:可能会遇到问题 微信支付错误问题的解决:curl出错,错误码:60 Fatal error: Unca ...
- virt-install详解
man virt-install VIRT-INSTALL() Virtual Machine Manager VIRT-INSTALL() NAME virt-install - provision ...
- jQuery选择器详解
根据所获取页面中元素的不同.可以将jQuery选择器分为:四大类,其中过滤选择器在分为六小类 jQuery选择器 基本选择器 层次选择器 过滤选择器 简单过滤选择器 内容过滤选择器 可见性过滤 ...
- Vue项目,运行出现warning(Emitted value instead of an instance of Error)
组件:<XXXX v-for="item in items" /> warning:(Emitted value instead of an instance of E ...
- nslookup和dig命令
nslookup与dig两个工具功能类似,都可以查询制定域名所对应的ip地址,所不同的是dig工具可以从该域名的官方dns服务器上查询到精确的权威解答,而nslookup只会得到DNS解析服务器保存在 ...