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 ...
随机推荐
- crm SDK 设置用户的上级
/// <summary> /// 设置用户的上级 /// </summary> /// <param name="service">服务< ...
- alt.js 使用教程
1.action : import alt from "../alt.js"; class DemoActions{ constructor() { this.generateAc ...
- 【LeetCode】3. 无重复字符的最长子串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc&qu ...
- linux:ubuntu安装mysql(二)--推荐
1)下载mysql安装包mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz,下载地址:https://dev.mysql.com/downloads/mysql/ 2 ...
- HTML中的GroupBox
<fieldset> <legend>用户登录</legend> <div class="box_a"> <label for ...
- 【Eclipse】Eclipse 安装 SVN 插件的三种方法
最近使用Eclipse时,修改代码总是看不到变化,不知道那个文件修改了,想起了当时开发C++的时候,有相关的插件,于是乎就网搜了下,果然有Eclipse的SVN配置. 接下来,我采用了第二种方式,一起 ...
- 34.scrapy解决爬虫翻页问题
这里主要解决的问题: 1.翻页需要找到页面中加载的两个参数. '__VIEWSTATE': '{}'.format(response.meta['data']['__VIEWSTATE']), '__ ...
- Linux将某目录授权给某组里的某用户
chown -Rf 用户名:组名 目录
- getent passwd 不能访问到 ldap 的用户
getent passwd 不能访问到 ldap 的用户,搞了一整个下午! 依然没搞定, 一开始是不知道nslcd 需要启动,另外getent passwd 域, 无有用结果, 换个方式搜索 get ...
- Mybatis七(MBG 逆向工程)
官方地址:http://www.mybatis.org/generator/ https://github.com/mybatis/generator/releases <1>编写mbg. ...