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数字组合的可能解。

解法1: 递归

解法2: 迭代

C++: Recursion

class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> res;
vector<int> out;
helper(n, k, 1, out, res);
return res;
}
void helper(int n, int k, int level, vector<int>& out, vector<vector<int>>& res) {
if (out.size() == k) res.push_back(out);
for (int i = level; i <= n; ++i) {
out.push_back(i);
helper(n, k, i + 1, out, res);
out.pop_back();
}
}
};

C++: Iteration

class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > res;
permuteDFS(num, 0, res);
return res;
}
void permuteDFS(vector<int> &num, int start, vector<vector<int> > &res) {
if (start >= num.size()) res.push_back(num);
for (int i = start; i < num.size(); ++i) {
swap(num[start], num[i]);
permuteDFS(num, start + 1, res);
swap(num[start], num[i]);
}
}
};

  

类似题目:

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

All LeetCode Questions List 题目汇总

[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. Example: I ...

  4. Leetcode 77, Combinations

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

  5. LeetCode 77,组合挑战,你能想出不用递归的解法吗?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode第46篇文章,我们一起来LeetCode中的77题,Combinations(组合). 这个题目可以说是很精辟了,仅仅 ...

  6. 高效率的全组合算法(Java版实现)

    博客上看到的一个算法,用Java实现了一个 算法描述: 算法说明:当n大于2时,n个数的全组合一共有(2^n)-1种. 当对n个元素进行全组合的时候,可以用一个n位的二进制数表示取法. 1表示在该位取 ...

  7. java 全组合 与全排列

    一.全组合 public static void Combination( ) { /*基本思路:求全组合,则假设原有元素n个,则最终组合结果是2^n个.原因是: * 用位操作方法:假设元素原本有:a ...

  8. 剑指offer-拓展训练-字符的所有组合-全组合

    /* 题目: 给定不含重复字符字符串的全组合. */ /* 思路: 递归法. 例给定abc,输出的组合长度为1,2,3. 对于长度为2的组合,分选择a(ab,ac)和不选择a的情况(bc). 选择a, ...

  9. 【JavaScript】Leetcode每日一题-组合总和4

    [JavaScript]Leetcode每日一题-组合总和4 [题目描述] 给你一个由 不同 整数组成的数组 nums ,和一个目标整数 target .请你从 nums 中找出并返回总和为 targ ...

随机推荐

  1. P4139 上帝与集合的正确用法[欧拉定理]

    题目描述 求 \[ 2^{2^{2\cdots}} ~mod ~p \] 简单题,指数循环节. 由于当\(b>=\psi(p)\)时,有 \[ a^b=a^{b ~mod~\psi(p)+\ps ...

  2. ZOJ - 3265: Strange Game (优化 二分图匹配)

    pro:有一个长度为N的数组a[i],要求选择k[i]>0,使得b[i]=a[i]^k[i]%M中出现的不同数最多.N<=200, M<=1e9: sol:a^x%p的个数的有限的, ...

  3. java SSM面试题

    1. 谈谈你mvc的理解MVC是Model—View—Controler的简称.即模型—视图—控制器.MVC是一种设计模式,它强制性的把应用程序的输入.处理和输出分开.MVC中的模型.视图.控制器它们 ...

  4. Kafka为什么这么快?

    批量处理 传统消息中间件的消息发送和消费整体上是针对单条的.对于生产者而言,它先发一条消息,然后broker返回ACK表示已接收,这里产生2次rpc:对于消费者而言,它先请求接受消息,然后broker ...

  5. Vue --- 组件练习

    一 ad_data = { tv: [ {img: 'img/tv/001.png', title: 'tv1'}, {img: 'img/tv/002.png', title: 'tv2'}, {i ...

  6. C++中指针形参问题

    1.C++指针做形参,会有很多陷阱,很多时候也许并不如我们想的那样.比如我们想通过一个函数改变指针的值: #include<</SPAN>iostream> using nam ...

  7. OLED液晶屏幕(3)串口读取文字并分割

    https://blog.csdn.net/iracer/article/details/50334041 String comdata = ""; void setup() { ...

  8. scylladb docker-compose 用户密码认证配置

    scylladb 对于用户的认证配置还是比较简单的,以下是一个docker-compose 配置的说明 环境准备 docker-compose 文件 version: "3" se ...

  9. DotNetty 常用库

    https://github.com/Azure/DotNetty DotNetty中几个重要的库(程序集): DotNetty.Buffers: 对内存缓冲区管理的封装. DotNetty.Code ...

  10. select count(1)和 select count(*)

    ),其实就是计算一共有多少符合条件的行. 1并不是表示第一个字段,而是表示一个固定值. 其实就可以想成表中有这么一个字段,这个字段就是固定值1,),就是计算一共有多少个1. 同理,),也可以,得到的值 ...