题目描述

找出所有相加之和为 的 个数的组合组合中只允许含有 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)的更多相关文章

  1. LeetCode 39. 组合总和(Combination Sum)

    题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限 ...

  2. Java实现 LeetCode 216. 组合总和 III(三)

    216. 组合总和 III 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合. ...

  3. [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 ...

  4. Leetcode 216. 组合总和 III

    地址 https://leetcode-cn.com/problems/combination-sum-iii/ 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并 ...

  5. [Swift]LeetCode40. 组合总和 II | Combination Sum II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  6. Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)

    Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...

  7. [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 ...

  8. [Leetcode 216]求给定和的数集合 Combination Sum III

    [题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...

  9. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

随机推荐

  1. Java并发编程——线程的基本概念和创建

    一.线程的基本概念: 1.什么是进程.什么是是线程.多线程? 进程:一个正在运行的程序(程序进入内存运行就变成了一个进程).比如QQ程序就是一个进程. 线程:线程是进程中的一个执行单元,负责当前进程中 ...

  2. haproxy + keepalived + mycat 高可用与负载均衡集群配置 centos7

    架构如上,但是其实keepalived.haproxy.Mycat都可以多台(比如keepalived.haproxy.Mycat各3台,3台keepalived抢占vip,然后抢到vip的hapro ...

  3. Java高并发程序设计学习笔记(十一):Jetty分析

    转自:https://blog.csdn.net/dataiyangu/article/details/87894253 new Server()初始化线程池QueuedThreadPoolexecu ...

  4. JVM学习笔记(一):Java虚拟机和虚拟机内存区域

    为什么Java程序需要运行在虚拟机上 因为Java在设计之初的跨平台特性,我们知道Java程序是运行在Java虚拟机上的.如果你要问为什么Java程序要运行在虚拟机上,我可以反问你几个问题. 为什么买 ...

  5. 实现代码重启android app.

    var Form1: TForm1; implementation uses System.DateUtils, Androidapi.JNI.GraphicsContentViewText, FMX ...

  6. TCP中异常关闭的情况记录

    1.当TCP连接的对端进程已经关闭了Socket的情况下,本端进程再发送数据时,第一包可以发送成功(但会导致对端发送一个RST包过来):之后如果再继续发送数据会失败,错误码为“10053: An es ...

  7. PAT Basic 1056 组合数的和 (15 分)

    给定 N 个非 0 的个位数字,用其中任意 2 个数字都可以组合成 1 个 2 位的数字.要求所有可能组合出来的 2 位数字的和.例如给定 2.5.8,则可以组合出:25.28.52.58.82.85 ...

  8. 使用VGG16完成猫狗分类

    from keras.applications.vgg16 import VGG16 from keras.models import Sequential from keras.layers imp ...

  9. mongodb单机搭建

    参考网站:http://www.runoob.com/mongodb/mongodb-linux-install.html 1.下载 https://www.mongodb.com/download- ...

  10. PHP底层运行机制与原理

    PHP的设计理念及特点 多进程模型:由于PHP是多进程模型,不同请求间互不干涉,这样保证了一个请求挂掉不会对全盘服务造成影响,当然,时代发展,PHP也早已支持多线程模型. 弱类型语言:和C/C++.J ...