一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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],

]

(二)解题

本题大意:给定两个整形数,n和k,从1~n中找出所有k个数的组合。

这题是典型的回溯问题。分析一下当n=4,k=3的情况:

维持一个vector变量,当它的长度小于3时,按顺序往里面放数字。

第一个满足长度等于3的组合为1,2,3,接下来回溯弹出3,继续放数字4

第二个满足长度等于3的组合为1,2,4,然后回溯弹出4,没有数字放了,就继续回溯弹出2,再放入3和4

第三个满足长度等于3的组合为1,3,4,然后回溯弹出3,4和1,放入2,3,4

第四个满足长度等于3的组合为2,3,4。至此,算法结束。

class Solution {
public:
    vector<vector<int>> ret;
    vector<vector<int>> combine(int n, int k) {
        vector<int> temp;
        dfscombine(n,1,k,temp);
        return ret;
    }
    void dfscombine(int n,int start,int k,vector<int> & temp)
    {
        if(temp.size()==k){//当长度为k时,将结果存在ret中
            ret.push_back(temp);
            return;
        }
        for(int i = start ; i <= n ; i++)
        {
            temp.push_back(i);//放入数字
            dfscombine(n,i+1,k,temp);
            temp.pop_back();//回溯到上一步
        }
    }
};

【一天一道LeetCode】#77. Combinations的更多相关文章

  1. LeetCode 77 Combinations(排列组合)

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

  2. [LeetCode] 77. Combinations 全组合

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

  3. Leetcode 77, Combinations

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

  4. [leetcode]77. Combinations组合

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

  5. leetCode 77.Combinations (组合)

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

  6. 【一天一道LeetCode】#17. Letter Combinations of a Phone Number

    一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...

  7. 【一天一道LeetCode】索引目录 ---C++实现

    [一天一道LeetCode]汇总目录 这篇博客主要收藏了博主所做题目的索引目录,帮助各位读者更加快捷的跳转到对应题目 目录按照难易程度:easy,medium,hard来划分,读者可以按照难易程度进行 ...

  8. 【一天一道LeetCode】#93. Restore IP Addresses

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

  9. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

随机推荐

  1. ftp:connect:未知错误号

    Linux下使用ftp命令时,提示:ftp: connect :未知错误号解决方法:service iptables stop或/etc/rc.d/init.d/iptables stop

  2. python学习之路网络编程篇(第四篇)- 续

    Memcache简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速 ...

  3. Throughtput收集器

    介绍 JVM里面的Throughtput收集器是一款关注吞吐量的垃圾收集器.该收集器是唯一一个实现了UseAdaptiveSizePolicy策略的收集器,允许用户通过指定最大暂停时间和垃圾收集时间占 ...

  4. 安卓高级8 SurfaceView (1)

    文章转载:http://www.cnblogs.com/xuling/archive/2011/06/06/android.html 首先我们先来看下官方API对SurfaceView的介绍 Surf ...

  5. shell编程--流程控制for,do-while,if-then,break,continue,case等

    2.5 流程控制 2.5.1 if语法 1.语法格式 if condition then     statements [elif condition     then statements. ..] ...

  6. Android N(7.0) 被美翻的新特性!

    Tamic 专注移动开发!更多文章请关注 Csdn: http://blog.csdn.net/sk719887916/article/details/52612444 $ http://www.ji ...

  7. java创建对象详解和多态问题

    一. java 构造方法不等于创建对象而是初始化对象,new 关键字分配内存和创建对象的.  二.Test test = new Test(); 有人用上面的表达式来说明构造方法返回对象引用,这是明显 ...

  8. Spark技术内幕: Shuffle详解(一)

    通过上面一系列文章,我们知道在集群启动时,在Standalone模式下,Worker会向Master注册,使得Master可以感知进而管理整个集群:Master通过借助ZK,可以简单的实现HA:而应用 ...

  9. 查看apk签名信息

    经常在注册开发者的时候会遇到要求填写申请应用的应用签名: 有两种很方便的方法: 1.如果没有源码或者没有打开eclipse,直接下载这个应用应用下载链接 使用截图,只要把包名输入,自动会出现签名信息. ...

  10. React Native开发工具Nuclide使用

    之前写RN的时候首选webstorm,这是之前做前端已经习惯的工具,其实RN开发官网推荐的是Nuclide工具, Nuclide是Fackbook专门为React开发IDE,今天也来尝试下,如果对we ...