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 transtion 实现分析
这是我用js和css3,实现的vue transition组件相同的效果核心js var btn = document.getElementById('btn'); var box = null bt ...
- jsonp的跨域原理
在开发测试中,难免会在不同域下进行跨域操作,出于安全性考虑,浏览器中的同源策略阻止从一个域上加载的脚本获取或者操作 另一个域下的文档属性,这时需要进行跨域的方式进行解决,如:使用jsonp ,ifra ...
- cookie、sessionStorage和localStorage区别
// 数据存储 cookie:生命周期一般是手动设置失效的时间,大小为4k,易用性不高,需要自己封装(封装请看上一篇博客) sessionStorage:生命周期是浏览器关闭接失效,大小为5m或者更大 ...
- webpack 学习过程
什么是WebPack,为什么要使用它? 为什要使用WebPack 现今的很多网页其实可以看做是功能丰富的应用,它们拥有着复杂的JavaScript代码和一大堆依赖包.为了简化开发的复杂度,前端社区涌现 ...
- 【Git的基本操作三】基本操作命令
基本操作 (1) 状态查看操作 git status 作用:查看工作区.暂存区状态 (2) 添加操作 git add [filename] 作用:将工作区文件的 添加/修改,添加到暂存区 (3) 提交 ...
- flutter: Another exception was thrown: Navigator operation requested with a context that does not include a Navigator.
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends State ...
- BLE 5协议栈-通用属性规范层(GATT)
文章转载自:http://www.sunyouqun.com/2017/04/page/2/ 通用属性规范GATT(Generic Attribute Profile)将ATT层定义的属性打包成不同的 ...
- 【C/C++】内存对齐规则和实战
内存对齐规则和实战 这篇文章是我的平时的一个笔记修改后来的.这里主要介绍一下内存对齐的规则,以及提供一些实战一下.几篇我觉得比较好的详细的介绍内存对齐的作用什么的博文会在文末附上. 规则 在开始实战前 ...
- 认识并初步应用GitHub
好好学习,天天向上 一.这是一个简单的开头 GIT的地址 https://github.com/Notexcellent GIT的用户名 Notexcxllent 学号后五位 82405 博客地址 h ...
- Twitter的支撑架构:扩展网络与存储并提供服务——架构原则:一次性将事情做对,NFL原则 LSM+B+存储替代cassandra
Twitter工程团队近期提供了Twitter核心技术的演进和扩展的详细资料,这些核心技术支撑了Twitter自营数据中心的系统架构,用于提供社会媒体服务.他们分享的关键经验包括:超越原始规格和需求进 ...