题目描述

给出两个整数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. .NET Standard SDK 样式项目中的目标框架

    系列目录     [已更新最新开发文章,点击查看详细] 包表示形式 .NET Standard 引用程序集的主要分发载体是 NuGet 包. 实现会以适用于每个 .NET 实现的各种方式提供. NuG ...

  2. sqlserver 分列

    sql server 数据库中某张表(Person)的数据信息是: ID Address 1 平山花园-4单元-12幢-203 2 香山花园-3单元-22幢-304 现在有需求是,将地址信息显示形式改 ...

  3. Apache Jmeter 性能测试

    今天在写性能测试报告的时候需要使用到数据,打算用做一下性能测试,然后在百度后发现了一款Apache开源的Jmeter压测工具 Jmeter概述: Apache JMeter是一款纯java编写负载功能 ...

  4. 2017年 实验五  B2B模拟实验

    实验五  B2B模拟实验 [实验目的] ⑴.掌握B2B中供应商的供求信息发布.阿里商铺开设和订单交易等过程. ⑵.掌握B2B中采购商的采购信息的发布.交易洽谈.网上支付和收货等过程. [实验条件] ⑴ ...

  5. CLion 控制台输出内容乱码问题的解决方法

    问题再现 #include "stdio.h" #include "stdlib.h" int main() { printf("嘤嘤嘤") ...

  6. MeteoInfoLab脚本示例:水汽通量散度计算

    用ncep数据计算水汽通量散度的脚本.需要air, uwnd, vwnd和rhum变量.数据是4维数据,需要固定时间维和高度维,数据中纬度维的数据是反向的,因此读取时需要特殊的设置(::-1).脚本中 ...

  7. 制作西北地区地图数据并maskout

    1.从全国地图数据中选中西北5省:打开bou2_4p.shp文件添加相应的图层(中国各省的行政区域),选中工具栏中的"通过矩形选择要素"工具,用鼠标点击选择要输出的图元,按住ctr ...

  8. 扫描仪扫描文件处理-图像扫描加工到生成PDF步骤简述[JAVA版]

    另参见:https://www.cnblogs.com/whycnblogs/p/8034276.html 详细见:https://github.com/barrer/scan-helper 用途: ...

  9. 多路查找树(2-3 树、2-3-4 树、B 树、B+ 树)

    本文参考自<大话数据结构> 计算机中数据的存储 一般而言,我们都是在内存中处理数据,但假如我们要操作的数据集非常大,内存无法处理了,在这种情况下对数据的处理需要不断地从硬盘等存储设备中调入 ...

  10. 解决python的requests库在使用过代理后出现拒绝连接的问题

    在使用过代理后,调用python的requests库出现拒绝连接的异常 问题 在windows10环境下,在使用代理(VPN)后.如果在python中调用requests库来地址访问时,有时会出现这样 ...