题目描述

给出两个整数n和k,返回从1到n中取k个数字的所有可能的组合
例如:
如果n=4,k=2,结果为
[↵  [2,4],↵  [3,4],↵  [2,3],↵  [1,2],↵  [1,3],↵  [1,4],↵]
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],↵]↵

示例1

输入

复制

2,1

输出

复制

[[1],[2]]
示例2

输入

复制

3,1

输出

复制

[[1],[2],[3]]

class Solution {
public:
    /**
     *
     * @param n int整型
     * @param k int整型
     * @return int整型vector<vector<>>
     */
    void DFS(vector <vector<int> >&ret,vector<int> &path,int n,int start,int rest){
        if (!rest)
            ret.push_back(path);
        else {
            for (int i=start;i<=n-rest+1;++i){
                path.push_back(i);
                DFS(ret,path,n,i+1,rest-1);
                path.pop_back();
            }
        }
    }
    vector<vector<int> > combine(int n, int k) {
        // write code here
        vector <vector<int>> ret;
        vector<int> path;
        DFS(ret,path,n,1,k);
        return ret;
        
    }
};

leetcode72:combinations的更多相关文章

  1. Combinations

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

  2. [LeetCode] Factor Combinations 因子组合

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

  3. [LeetCode] Combinations 组合项

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

  4. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  5. Leetcode 254. Factor Combinations

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

  6. 17. Letter Combinations of a Phone Number

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  7. LeetCode——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  8. Combination Sum II Combinations

    https://leetcode.com/problems/combination-sum-ii/ 题目跟前面几道题很类似,直接写代码: class Solution { public: vector ...

  9. No.017:Letter Combinations of a Phone Number

    问题: Given a digit string, return all possible letter combinations that the number could represent.A ...

随机推荐

  1. 怀疑安装MySQL之后,导致OrCAD Capture、Allegro就打不开

    记得在异常出现之前,只安装了MySQL,之后OrCAD Capture.Allegro就打不开了. Capture.exe - 系统错误 allegro.exe - 系统错误 我尝试在Cadence的 ...

  2. NOIP提高组2016 D2T3 【愤怒的小鸟】

    貌似还没有写过状压DP的题目,嗯,刚好今天考了,就拿出来写一写吧. 题目大意: 额,比较懒,这次就不写了... 思路分析: 先教大家一种判断题目是不是状压DP的方法吧. 很简单,那就是--看数据范围! ...

  3. Python 的映射数据类型有哪些?零基础小白入门学习必看

    1 映射类关系 Python 的 collections.abc 模块内拥有 Mapping 和 MutableMapping 这两个抽象基类,它们为 dict 和其他类似的类型提供了接口定义. mu ...

  4. CentOS7 执行 service iptables save 报错 The service command supports only basic LSB actions xxxxxx

    现象描述 在 CentOS 7.6.1810 下执行 service iptables save 命令,出现如下错误: [root@test ~]# service iptables save The ...

  5. python 利用jieba库词频统计

    1 #统计<三国志>里人物的出现次数 2 3 import jieba 4 text = open('threekingdoms.txt','r',encoding='utf-8').re ...

  6. 程序员,想被别人发掘?那你有 freestyle 吗?

    程序员群体是偏内向的,整天和计算机打交道,用代码说话,接受任务,默默工作. 如果这些任务是有挑战性的还行,你的工作成果就能帮你说话,可是大部分工作都是普普通通的,甚至有点儿重复性的劳动. 这个时候表面 ...

  7. 【博弈论】CF 1215D Ticket Game

    题目大意 洛谷链接 给出一个长度为\(n\)的由数字组成的字符串(\(n\)是偶数).但可能有偶数个位上的数字为?. 现在有两个人\(A\)和\(B\),在?的位置上填\(0\)~\(9\)的数,一直 ...

  8. 安装Linux注意事项

    网络配置NAT Worstation 生成虚拟网卡,编辑虚拟网络中子网IP地址为10网段内部地址,避免冲突.  Linux命令 查看主机IP地址 [root@C8 ~]# hostname -I 19 ...

  9. ERP仓库管理的操作与设计--开源软件诞生20

    赤龙ERP库房管理讲解--第20篇 用日志记录"开源软件"的诞生 [点亮星标]----祈盼着一个鼓励 博主开源地址: 码云:https://gitee.com/redragon/r ...

  10. HTML轮播(2)

    前言 现在在完成轮播的框架上进行扩展更多的功能,上下切换图片,以及添加动画滚动更加平滑过渡 CSS <style> #LB { width: 100%; height: 948px; ov ...