题目

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. (转)CLR20R3 程序终止的几种解决方案

    原文地址:https://blog.csdn.net/fxfeixue/article/details/4466899 这是因为.NET Framework 1.0 和 1.1 这两个版本对许多未处理 ...

  2. 安装Thinkphp5

    Thinkphp完全开发手册地址:https://www.kancloud.cn/manual/thinkphp5/118006 1.安装扩展mbstring扩展:sudo apt-get insta ...

  3. JVM总结-Java语法糖与Java编译器

    自动装箱与自动拆箱 首先要提到的便是 Java 的自动装箱(auto-boxing)和自动拆箱(auto-unboxing). 我们知道,Java 语言拥有 8 个基本类型,每个基本类型都有对应的包装 ...

  4. WPF圆角按钮与触发颜色变化

    <Button x:Name="button1" Content="按钮1" Margin="10,10,0,0" Cursor=&q ...

  5. vuejs实现瀑布流布局(三)

    前面写过vuejs实现的瀑布流布局,<vuejs实现瀑布流布局(一)>和<vuejs实现瀑布流布局(二)>也确实实现了瀑布流布局,但是这个是基于SUI-Mobile实现的无限滚 ...

  6. for循环执行时在,每执行一次for循环中弹出提示框,延时问题

    在需求中,ajax的返回值,根据数组内容的长度去做循环,每循环一次弹出提示框,发现for循环的执行速度非常之快,想到了延时,但是在for循环中延时并不能解决这个问题. 查到setTimeout的递归处 ...

  7. StanFord ML 笔记 第一部分

    本章节内容: 1.学习的种类及举例 2.线性回归,拟合一次函数 3.线性回归的方法: A.梯度下降法--->>>批量梯度下降.随机梯度下降 B.局部线性回归 C.用概率证明损失函数( ...

  8. html A标签 绑定点击事件。跳转页面。处理

    在平时的页面中,肯定有需要点击A标签 进行处理. 这时候习惯性的绑定一个点击事件进行数据处理. 在A标签中 herf="#" 然后绑定一个点击事件. 或者在A标签里面的元素里面有一 ...

  9. Java-1.6-并发包-集合

    List相关 对于List并发包就只扩展了一个CopyOnWriteArrayList<E> Set相关 对于Set,并发包扩展ConcurrentSkipListSet<E> ...

  10. CMD定时倒数

    修改if %count%==20 goto finish改变秒数 CMD: mode con: cols=80 lines=25color 4ftitle Please WaitSET /P var= ...