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 ...
随机推荐
- web录音——上传录音文件
捕获麦克风 一. 前言 公司项目需要实现web录音,刚刚好接手此功能,由于之前未接触过,在网上找了些资料做对比 ) https://www.cnblogs.com/starcrm/p/51 ...
- vue入门 0 小demo (挂载点、模板、实例)
vue入门 0 小demo (挂载点.模板) 用直接的引用vue.js 首先 讲几个基本的概念 1.挂载点即el:vue 实例化时 元素挂靠的地方. 2.模板 即template:vue 实例化时挂 ...
- 分布式系统读写模型中的Quorum机制
分布式系统的设计中会涉及到许多的协议.机制用来解决可靠性问题.数据一致性问题等,Quorum 机制就是其中的一种.我们通过分布式系统中的读写模型来简单介绍它. 分布式系统中的读写模型 分布式系统是由多 ...
- Oracle笔记(四) 简单查询、限定查询、数据的排序
一.简单查询 SQL(Structured Query Language) 结构化查询语言,是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理关系数据库系统.ANSI(美国国家标准学会) ...
- 十,StatefulSet简介及简单使用
目录 StatefulSet简介 为什么要用statefulset控制器 简单测试 使用 StatefulSet 创建基础的PV StatefulSet 清单 statefulset管理pod的启停顺 ...
- python常用模块:模块练习
今日作业: 1.简述 什么是模块 模块就将一些函数功能封装在一个文件内,以‘文件名.py’命名,以“import 文件名”方式调用 模块有哪些来源 自定义.内置.DLL编译器.包模块的格式要求有哪些 ...
- three.js之创建坐标系网格
<!DOCTYPE html> <html> <head> <meta charset=utf-8> <title>My first thr ...
- LeetCode 01 两数之和
链接:https://leetcode-cn.com/problems/two-sum 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们 ...
- 小程序wx.showToast()方法实现文字换行
小程序wx.showToast()方法实现文字换行 在文字中间加上 '\r\n' 真机中生效 wx.showToast({ title: `换行前内容\r\n换行后内容`, icon: 'none' ...
- BZOJ 2119: 股市的预测 (Hash / 后缀数组 + st表)
转博客大法好 自己画一画看一看,就会体会到这个设置关键点的强大之处了. CODE(sa) O(nlogn)→1436msO(nlogn)\to 1436msO(nlogn)→1436ms #inclu ...