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 ...
随机推荐
- 【ExtJs】获取grid选中的records
var records = me.grid.getSelectionModel().getSelection(); //获取所有选中的行 var record =records[0]; //获取选中行 ...
- element-ui Cascader 级联选择器 点击label选中
通过修改label的样式解决: 注意:el-cascader-panel 是直接挂载在body上的,所以需要全局设置 .el-cascader-panel .el-radio{ width: 100% ...
- docker容器里面安装php的redis扩展
docker exec -i -t php /bin/bash 进入php容器内执行:pecl install -o -f redis 修改php.ini,添加:extension=redis. ...
- QQ大盗 - 巧用clientkey
场景: 1.将程序发给好友,好友打开 qq昵称就会被秒改为”账号已被盗“. 2.将程序运行在自己的电脑,让那些随意借用电脑看片聊天的室友产生一个觉悟:乱使用别人电脑很可能会泄露隐私. 思路: 通过数据 ...
- remote mounting from windows to linux
8 Ways To Mount SMBfs (SAMBA FILE SYSTEM) In Linux. Sep 8, 2009 How to Mount smbfs (SAMBA file syste ...
- Jenkins升级版本
1 Jenkins的管理界面,下载最新版本的war包 2 找到自己部署Jenkins的war包的tomcat目录,替换最新的war包,重启tomcat即可 只需要把之前的war包重命名一个名字,不要以 ...
- 简单粗暴 每个servlet之前都插入一段代码解决 乱码问题
response.setHeader("content-type", "text/html;charset=UTF-8"); response.setChara ...
- 十四,K8s集群网络flannel及canal策略
目录 k8s网络CNI之flannel k8s网络通信模型 常见CNI插件(Container,Network,Interface) 插件通信一般的解决方案 网络插件的应用 Flannel插件 fla ...
- Go语言基础之操作MySQL
Go语言操作MySQL MySQL是常用的关系型数据库,本文介绍了Go语言如何操作MySQL数据库. Go操作MySQL 连接 Go语言中的database/sql包提供了保证SQL或类SQL数据库的 ...
- 23_2spring的常用注解
1.基于注解的IOC配置 1.1导入jar包 <?xml version="1.0" encoding="UTF-8"?> <project ...