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 ...
随机推荐
- vue打包后css背景图片地址找不到
背景图片变成了这样:static/css/static/imgs/xxx.jpg 解决方法,修改build/utils,添加 publicPath: '../../' 就行 对比了下,com ...
- day1-css练习[新浪首页顶部栏]
直接贴代码吧: html代码 <div class="border-01"> <div class="border-001"> < ...
- vue 项目中如何在页面刷新的状态下保留数据
1.问题:在vue项目中,刷新页面之后,我当前打开的所有菜单,都消失,我如何实现刷新之后页面仍然是刷新之前的状态 效果图: 解决方法: 使用vuex作状态管理: 将vuex里面的数据同步更新到loca ...
- 上传图片,语音,和富文本(webuploader,dropzone, froala)
首先是上传图片,使用的百度webuploader 自己修改后可以实例化多个uploader对象: HTML: <!DOCTYPE html> <html xmlns="ht ...
- Delphi 图形图像对象组件
- jmeter 压力测试tcp
cmd下管理员执行 jmeter 界面 英文版 中文切换以后不能执行 最多跑905个线程 线程限制 查看结果数 界面会跑死 windows环境 注意:测试1万的tcp并发连接 大爷的 window最 ...
- css3属性transform-origin属性讲解
transform是CSS3里的变换属性,常用的有translate(平移).rotate(旋转).skew(倾斜).scale(缩放)方法.而transform-origin并不是transform ...
- Python3-Set
# Set(集合) # 集合(set)是一个无序不重复元素的序列. # 基本功能是进行成员关系测试和删除重复元素. # 可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须 ...
- PHP中使用PDO的预处理功能避免SQL注入
不使用预处理功能 <?php $id = $_GET['id']; $dsn = 'mysql:host=localhost;port=3306;dbname=database'; try { ...
- 卸载python
# rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps # whereis python|xargs rm -frv