题目描述

给出两个整数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. ASP。NET Web表单模型,部分呈现和事件

    下载EventExample.zip - 41.33 KB 下载EventandAjaxExample.zip - 41.94 KB 介绍 通过参考ASP获得Web应用程序环境及其约束的概述.NET ...

  2. Flink实例(五十): Operators(十)多流转换算子(五)coGroup 与union

    参考链接:https://mp.weixin.qq.com/s/BOCFavYgvNPSXSRpBMQzBw 需求场景分析 需求场景 需求诱诱诱来了...数据产品妹妹想要统计单个短视频粒度的「点赞,播 ...

  3. HTML常用标签(下)

    HTML常用标签(下) 1. 表格标签 1.1 语法 <table> <!--table定义表格--> <tr> <!--tr定义表格中的行--> &l ...

  4. OpenCV开发笔记(七十):红胖子带你傻瓜式编译VS2017x64版本的openCV4

    前言   红胖子来也!!!  opencv_contrib是opencv提供额外的工具,提供一些基础算法,之前编译了不带opencv_contrib的版本,不带opencv_contrib的cuda硬 ...

  5. day12 Pyhton学习

    一.昨日内容回顾 1.函数名 函数名是一个变量名 可以作为集合类的元素 可以作为参数进行传递 def  func(fn): fn() 可以作为返回值返回 def outer(): def inner( ...

  6. day08 Pyhton学习

    一.昨日内容回顾 .1.基础部分的补充 join()  把列表变成字符串, 拼接 split() 切割 删除: 列表和字典不能在循环的时候进行删除. 把要删除的内容记录在一个新列表中,然后循环新列表, ...

  7. 对json数组按照id精确查询并修改值

    //json数组,里面有一个id等于5的,班级的标识和名称不是该班级,通过id把班级信息修改为指定的信息 var zNodes=[ { id:1, classid:1, className:" ...

  8. set的运用 例题5-3 安迪的第一个字典(Andy's First Dictionary,Uva 10815)

    #include<bits/stdc++.h>using namespace std;set<string> dict;int main(){ string s, buf; w ...

  9. selenium环境配置学习笔记

    一 为什么进行自动化测试 缩短测试周期 避免人为出错 测试信息存储 轻易获取覆盖率 二 web/ui自动化条件和适用范围 手工测试已经完成,后期在不影响进度的前提下逐渐实现自动化 项目周期长,重复性工 ...

  10. pmm-server 搭建

    1 搭建docker centos 下 参考文档搭建docker  https://www.cnblogs.com/brady-wang/p/11543237.html docker create \ ...