LeetCode 216. 组合总和 III(Combination Sum III)
题目描述
找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。
说明:
- 所有数字都是正整数。
- 解集不能包含重复的组合。
示例 1:
输入: k = 3, n = 7
输出: [[1,2,4]]
示例 2:
输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]
解题思路
回溯算法,从第一个数开始依次添加数字并比较当前数字总和,若相等就添加到结果集合中。
代码
class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> res;
if(k <= || n <= ) return res;
vector<int> nums, temp;
for(int i = ; i < ; i++)
nums.push_back(i + );
combine(nums, , , k, n, temp, res);
return res;
}
void combine(vector<int> nums, int idx, int sum, int k, int n, vector<int> &temp, vector<vector<int>> &res){
if(k == && sum == n)
res.push_back(temp);
else if(sum < n){
for(int i = idx; i < ; i++)
if(sum + nums[i] <= n){
temp.push_back(nums[i]);
combine(nums, i + , sum + nums[i], k - , n, temp, res);
temp.pop_back();
}
}
}
};
LeetCode 216. 组合总和 III(Combination Sum III)的更多相关文章
- LeetCode 39. 组合总和(Combination Sum)
题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限 ...
- Java实现 LeetCode 216. 组合总和 III(三)
216. 组合总和 III 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合. ...
- [Swift]LeetCode216. 组合总和 III | Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Leetcode 216. 组合总和 III
地址 https://leetcode-cn.com/problems/combination-sum-iii/ 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并 ...
- [Swift]LeetCode40. 组合总和 II | Combination Sum II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)
Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [Leetcode 216]求给定和的数集合 Combination Sum III
[题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
随机推荐
- webpack的postcss的基本应用
PostCss是什么? PostCSS在webpack中的基本应用 一.PostCss是什么? 如果有深入学习PostCss需求的话可以参考大漠的资料:https://www.w3cplus.com/ ...
- cookie、sessionStorage和localStorage区别
// 数据存储 cookie:生命周期一般是手动设置失效的时间,大小为4k,易用性不高,需要自己封装(封装请看上一篇博客) sessionStorage:生命周期是浏览器关闭接失效,大小为5m或者更大 ...
- 99乘法表(js)
//九九乘法表 let i,j,str; for(i=1;i<=9;i++) { str = ""; for(j=1;j<=i;j++) { str = str+i+' ...
- 1 .net中自定义事件的步骤
1 申明一个自定义的类并且继承事件的基类 public class ClientSocketModelConnectedEvent:EventArgs { private string param; ...
- mac 下开发golang 配置
1.安装golang 见附件 2.默认安装在 /usr/local/go 目录下 3.配置环境变量: 编辑文件:vim /etc/profile,有的MAC 下没有这个文件,可以新建. 加入环境变量 ...
- 转载--Java中的PO、DO、DTO、 VO的概念
Java中的PO.DO.DTO. VO的概念 写的很清晰,学习了.
- php 获取网址参数
echo "rewrite: ".$_GET["rewrite"]; echo "<br>SERVER_PORT: ".$_SE ...
- thinkphp5.0 column多字段问题
一个字段:返回一维数组,数字索引为键名: 二个字段:返回一维数组,第一个字段为键名,第二个字段为元素值: 三个或更多字段:返回二维数组,第一个字段为键名,全部字段值为数据元素: 指定键名:方法的第二个 ...
- 1.Bacula各组件说明
1. Bacula各组件说明 Baula有三个服务,分别是bacula-sd用于管理storage.bacula-fd为bacula客户端.bacula-dir为bacula的核心组件机direct ...
- 4.NIO_Channel 通道
1.通道(Channel) 由 java.nio.channels 包定义的.Channel 表示 IO 源与目标打开的连接.Channel 类似于传统的“流”.只不过 Channel 本身不能直接访 ...