题目

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的更多相关文章

  1. 【LeetCode】77. Combinations 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  2. 【一天一道LeetCode】#77. Combinations

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  3. 【LeetCode】77. Combinations (2 solutions)

    Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ...  ...

  4. LeetCode OJ:Combinations (排列组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  5. LeetCode 77 Combinations(排列组合)

    题目链接:https://leetcode.com/problems/combinations/#/description    Problem:给两个正数分别为n和k,求出从1,2.......n这 ...

  6. [LeetCode] 77. Combinations 全组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  7. Leetcode OJ 刷题

    Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...

  8. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  9. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

随机推荐

  1. 知识点:linux数据库备份

    服务端启用二进制日志 如果日志没有启开,必须启用binlog,要重启mysql,首先,关闭mysql,打开/etc/my.cnf,加入以下几行: [mysqld] log-bin 然后重新启动mysq ...

  2. 安装sublime txt3 并且设置为默认的text打开方式

    1.安装 安装可以参考 http://jingyan.baidu.com/article/fa4125acb8569b28ac7092ea.html 1.添加sublime text 3的仓库: su ...

  3. MySQL的用户密码过期功能

    Payment Card Industry,即支付卡行业,PCI行业表示借记卡.信用卡.预付卡.电子钱包.ATM和POS卡及相关的业务. PCI DSS,即PCI数据安全标准(Payment Card ...

  4. MySQL设置远程连接

    Window下MySQL设置开启远程连接mysql数据库 1.新建用户远程连接mysql数据库grant all on *.* to admin@'%' identified by '123456' ...

  5. OpenJudge 求重要逆序对数

    https://blog.csdn.net/mrvector/article/details/81090165 [题解] 方法与求逆序对的个数类似,用归并排序分治求解.不同之处在于添加了一个虚拟指针p ...

  6. C# 自己动手实现Spy++(二)

    昨天已经实现了获取窗口的标题.句柄等信息,但是高亮部分还有问题,而且红色绘制框擦除也有问题,今天就又研究了下上述两个问题. 高亮部分红色框只显示左上的边框,而右下的显示不出来,如图: 代码如下: pu ...

  7. (转)Floyd算法

    原文地址:http://www.cnblogs.com/twjcnblog/archive/2011/09/07/2170306.html 参考资料:http://developer.51cto.co ...

  8. HBase数据模型和读写原理

    Hbase的数据模型和读写原理: ​ HBase是一个开源可伸缩的分布式数据库,他根据Google Bigtable数据模型构建在hadoop的hdfs存储系统之上. ​ HBase是一个稀疏.多维度 ...

  9. BZOJ 3473: 字符串 (广义后缀自动机)

    /* 广义后缀自动机, 每次加入维护 该right集合的set, 然后可以更新所有的parent,最终能够出现在k个串中right集合也就是set大小大于等于k的部分 这样的话就给了我们要跳的节点加了 ...

  10. Struts2学习:Action获取properties文件的值

    配置文件路径: 配置内容: 方法一: Action内被调用的函数添加下段代码: Properties props = new Properties(); props.load(UploadFileAc ...