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 ...
随机推荐
- log4net 配置允许同时写日志到同一个文件
RollingFileAppender appender = new RollingFileAppender();... appender.LockingModel = new FileAppende ...
- MySQL操作mysqldump命令详解
--all-databases , -A导出全部数据库. --all-tablespaces , -Y导出全部表空间. --no-tablespaces , -y不导出任何表空间信息. --add-d ...
- 主机、Docker时间与时区设置总结
最近在使用Docker容器时,部署java程序发现时间输出不对,在修改问题时总结如下. #date [-R] #查看主机时间 #timedatectl #查看主机时区 #tzselect ...
- Delphi 解决Utf8ToAnsi和Utf8DeCode转换编码为空的问题
//delphi DecodeUtf8Str解决系统自带UTF8解码缺陷 function DecodeUtf8Str(const S: UTF8String): WideString; var le ...
- windows 查询文件被什么程序占用
运行Resmon CPU选项卡全选 在[关联的句柄]里查询: 需要的时间挺多的...
- tips:Java中while的判断条件
tips:Java中while的判断条件! 在c++中,有时候会遇到这种情况: while(x = y){ dosomething; } 如果x与y相等,这个时候如果循环体中没有跳出的点,那么会无限循 ...
- JS弹框
<script type="text/javascript"> function show_alert() { alert('警报'); } function show ...
- Android Studio快捷键Ctrl+Shift+F不能用,全局搜索不能用;
AS全局搜索Ctrl+Shift+F突然就不能用了,在AS找半天没有找到问题,原因竟然是和搜狗输入法的简繁切换冲突了:下面有图把简繁切换关闭或更换快捷键后,as的全局搜索就能用了:
- CF1017G The Tree
/* 这是什么神仙题目QAQ 首先考虑在序列上的问题 先不考虑修改成白色, 一个白点能r被染成黑色 意味着能够找到一个l使得在l-r中的操作1次数大于等于 r - l + 1 我们把初始值覆盖成-1就 ...
- C# .NET ToList()分页
ViewData.Model = Limodel.ToList().Skip((page - ) * ).Take().ToList(); //page 当前页,10:每页几条数据