题目

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. 00001 - Linux下 环境变量/etc/profile、/etc/bashrc、~/.bashrc的区别

    ①/etc/profile: 该文件登录操作系统时,为每个用户设置环境信息,当用户第一次登录时,该文件被执行.也就是说这个文件对每个shell都有效,用于获取系统的环境信息. # /etc/profi ...

  2. MySQL 8.0用户和角色管理

    MySQL 8.0用户和角色管理 MySQL8.0新加了很多功能,其中在用户管理中增加了角色的管理,默认的密码加密方式也做了调整,由之前的sha1改为了sha2,同时加上5.7的禁用用户和用户过期的设 ...

  3. sklearn获得某个参数的不同取值在训练集和测试集上的表现的曲线刻画

    from sklearn.svm import SVC from sklearn.datasets import make_classification import numpy as np X,y ...

  4. 转]GSM模块信号强度CSQ与RSSI的对应关系

    使用GSM或者3G模块时,都会接触到信号强度CSQ.通过指令AT+CSQ,模块返回当前的信号质量,例如: AT+CSQ +CSQ: 28,0 其中28就是信号强度CSQ,但它不是真实的CSQ,他应该叫 ...

  5. FIN omitted, FIN-ACK sent

    STACKOVER ADDRESS:https://stackoverflow.com/questions/21390479/fin-omitted-fin-ack-sent question: As ...

  6. 基于pyQt5开发的股价显示器(原创)

    #/usr/bin/env python # -*- coding: utf-8 -*- ''' @author="livermorium116" 为了绕开公司内网而开发的 股票实 ...

  7. java开发_""和null的区别

    转自:http://www.cnblogs.com/hongten/archive/2012/11/08/java_null.html#undefined 在代码中: 1 //name可以为" ...

  8. 【Python爬虫实战】 使用代理服务器

    代理服务器:是一个处于我们与互联网中间的服务器,如果使用代理服务器,我们浏览信息的时候,先向代理服务器发出请求,然后又代理服务向互联网获取信息,再返回给我们使用代理服务器进行信息爬取,可以很好的解决I ...

  9. 使用swagger 生成 Flask RESTful API

    使用swagger 生成 Flask RESTful API http://www.voidcn.com/article/p-rcvzjvpf-e.html swagger官网 https://swa ...

  10. Maven的下载和配置

    一.下载 打开 链接地址 http://maven.apache.org/download.cgi 下载 Maven, 下载加压打开以后目录: 二.配置环境变量 我的电脑 -> 属性->高 ...