题目

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. 阿里云发送短信验证码php_SDK

    1.登录阿里云账号下载——aliyun-dysms-php-sdk(我使用的php版本) 下载地址:https://help.aliyun.com/document_detail/55359.html ...

  2. Java第一次上机实验源代码

    小学生计算题: package 第一次上机实验_; import java.util.*; public class 小学计算题 { public static void main(String[] ...

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

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

  4. vue使用animate.css库

    //引入库 <link rel="stylesheet" type="text/css" href="animate.css"> ...

  5. 零基础学习python_字典(25-26课)

    今天学到后面的知识,突然发现之前学习到的字典列表啥的都有点忘了,打算补一下之前学到的字典,到时候你看的时候,字符串.列表.字典.元祖这几个没啥顺序,刚开始学的时候了解下方法,当然你可以死记硬背下,后面 ...

  6. 【Selenium-WebDriver自学】WebDriver交互代码(十一)

    ==================================================================================================== ...

  7. Java Swing类 颜色、按键状态判断例子代码

    package rom; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; ...

  8. oracle TDE

    转自:oracle TDE学习系列 (1) — wallet 使用管理 关于oracle wallet,通常称为oracle钱夹,说的通俗一点,oracle wallet是一个用 口令加密的PKCS# ...

  9. Robot Operating System (ROS)学习笔记---创建简单的机器人模型smartcar

    搭建环境:XMWare  Ubuntu14.04  ROS(indigo) 转载自古月居  转载连接:http://www.guyuehome.com/243 一.创建硬件描述包 已创建catkin_ ...

  10. Linux网络管理-相关笔记【自用】

    ISO/OSI七层模型应用层            APDU 应用层协议数据单元   越靠近用户表示层            PPDU 表示层协议数据单元会话层            SPDU 会话协 ...