LeetCode 77. 组合(Combinations)
题目描述
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
示例:
输入: n = 4, k = 2
输出:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
解题思路
回溯法,每次遍历到一个元素分为放入与不放入集合两种情况,若集合长度为k,则加入到结果中。
代码
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> res;
vector<int> nums, temp;
for(int i = ; i < n; i++)
nums.push_back(i + );
cmb(n, k, , nums, temp, res);
return res;
}
void cmb(int n, int k, int idx, vector<int> nums, vector<int> &temp, vector<vector<int>> &res){
if(k && temp.size() == k)
res.push_back(temp);
else if(idx < n){
temp.push_back(nums[idx]);
cmb(n, k, idx + , nums, temp, res);
temp.pop_back();
cmb(n, k, idx + , nums, temp, res);
}
}
};
LeetCode 77. 组合(Combinations)的更多相关文章
- Java实现 LeetCode 77 组合
77. 组合 给定两个整数 n 和 k,返回 1 - n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...
- Leetcode之回溯法专题-77. 组合(Combinations)
Leetcode之回溯法专题-77. 组合(Combinations) 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输 ...
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- [LeetCode] 77. Combinations 全组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- LeetCode:组合总数III【216】
LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- leetcode排列组合相关
目录 78/90子集 39/40组合总和 77组合 46/47全排序,同颜色球不相邻的排序方法 78/90子集 输入: [1,2,2] 78输出: [[], [1], [2], [1 2], [2], ...
- leetCode 77.Combinations (组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [leetcode]77. Combinations组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
随机推荐
- Myeclipse启动后tomcat空指针异常
今天早上吃完早餐来公司上班,打开电脑,输入密码,123456.....嗯……,再打开myeclipse,duang...duang...duang....tomcat空指针异常,tmd我这暴脾气昨天还 ...
- JavaScript的常用浏览器设置
用什么浏览器?如果您不告诉我您使用的浏览器,我将告诉您有关JavaScript的常用浏览器设置.~火狐在菜单栏中选择工具->选项->内容以查看启用javascript的选项.Interne ...
- JavaWeb【JSTL】
根据JSTL标签所提供的功能,可以将其分为5个类别. 核心标签 格式化标签 SQL 标签 XML 标签 JSTL 函数 使用方式 1.下载包 地址:http://archive.apache.org/ ...
- Win7自带的系统备份还原功能如何去使用?
很多用户都会反映Win7系统使用过程中会出现系统或应用程序方面的小故障,针对这些小问题,再选择进行电脑系统的重装就有些过于麻烦了. 其实Win7系统内带有系统备份和还原的功能,可以在电脑系统出现小问题 ...
- 第六章·Logstash深入-收集java日志
1.通过Logstash收集java日志并输出到ES中 因为我们现在需要用Logstash收集tomcat日志,所以我们暂时将tomcat安装到Logstash所在机器,也就是db03:10.0.0. ...
- 超简单!教你如何修改源列表(sources.list)来提高软件访问速度
因为Ubuntu官方的源地址不在国内,所以在国内的访问速度非常慢,比如:我们要下载或是更新软件那速度比蜗牛还慢.所以,我们需要改成国内的镜像服务器,这样,我们在下载或更新软件的时候就会很快了. 配置步 ...
- ScrollView 滚动视图
ScrollView 种类: 1.HorizontalScrollView:水平滚动视图 2.ScrollView:垂直滚动视图(常用类) public class MainActivity exte ...
- CSS属性(pading margin)
margin: margin:5px auto;意思上下为5,左右平均居中 margin-top: 20px; 上外边距 margin-right: 3 ...
- Naming Company CodeForces - 794C (博弈,构造)
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...
- div 可滚动但不显示滚动条
(原) 首先有3个div, 第1个,固定大小是200*200(单位为px,下同) 第2个,不固定大小,其大小要用第3个div把个撑开,但是这个div必需要有滚动条, 第3个,固定大小与第1个div保持 ...