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 ...
随机推荐
- 如何优雅地等待所有的goroutine退出
转自:https://www.cnblogs.com/cobbliu/p/4461866.html goroutine和channel是Go语言非常棒的特色,它们提供了一种非常轻便易用的并发能力.但是 ...
- pandas函数get_dummies的坑
转载:https://blog.csdn.net/mvpboss1004/article/details/79188190 pandas中的get_dummies得到的one-hot编码数据类型是ui ...
- [电脑知识点]win10家庭版怎么显示桌面图标
控制面板-------->外观和个性化-------->个性化-------->更改桌面图标-------->然后你就能看到你想要的东西了,勾选上就可以了
- windows gitbook pdf
1.安装 nodejs 下载地址:https://nodejs.org/download/release/v6.9.2/node-v6.9.2-x64.msi 执行安装 配置环境变量:C:\Progr ...
- HDFS分布式文件系统
hadoop致力于构建在廉价的商用服务器上 多副本存储策略(副本数存多少合适) 常见是数据访问方式:流式数据访问(更适合大数据的访问) 随机数据访问(更适合传统的关系型数据库的访问)
- BZOJ3812主旋律
/* 这道题其实没有看懂 所以整理一下吧 首先思想转化成所有方案减去不强联通的方案 不强联通的方案相当于很多强联通分量缩点后的dag 转化成子问题, 问很多点的dag方案数 然后枚举作为出度为0的点集 ...
- fiddler--配置火狐代理配置代理
fiddler安装 下载fiddler最新版: 默认安装: 配置火狐代理配置代理 打开fiddler 按图操作,复制选中的链接:fiddler -> tool ->connections ...
- kafka 消息系统
一.为什么需要消息系统 1.解耦: 允许你独立的扩展或修改两边的处理过程,只要确保它们遵守同样的接口约束. 2.冗余: 消息队列把数据进行持久化直到它们已经被完全处理,通过这一方式规避了数据丢失风险. ...
- day3----编码-集合-深浅copy-文件操作-函数初识
本文档主要内容: 一 编码 二 集合 三 深浅copy 四 文件操作 五 函数初识 首先,我们来看看两个字符串的比较 打开cmd,进入dos界面 >>>a='xingchen@' & ...
- switch的穿透,是参数里包含case内容就执行。
package rom; import java.lang.*; /* * switch的穿透,是参数里包含case内容就执行. */ public class Xamle_5 { public st ...