题目描述

找出所有相加之和为 的 个数的组合组合中只允许含有 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. Linux--目录属性

    目录的读属性:表示具有读取目录结构清单的权限.使用ls命令可以将该目录中的文件和子目录的内容列出来. 目录的写属性:表示具有更改目录结构清单的权限.包括以下操作: 建立新的文件与目录 删除已经存在的文 ...

  2. python连接oracle导出数据文件

    python连接oracle,感觉table_list文件内的表名,来卸载数据文件 主脚本: import os import logging import sys import configpars ...

  3. python eval内置函数作用

    功能:将字符串str当成有效的表达式来求值并返回计算结果. 语法: eval(source[, globals[, locals]]) -> value 参数: source:一个Python表 ...

  4. Oracle【三表的联合查询】

    ,'北京','彰显大气'); ,'上海','繁华都市'); ,'广州','凸显舒适'); ,'深圳','年轻气氛'); ,'北上广深','不相信眼泪'); commit; ; ; ; ; ; 员工信息 ...

  5. BLE 5协议栈-安全管理层

    文章转载自:http://www.sunyouqun.com/2017/04/ 安全管理(Security Manager)定义了设备间的配对过程. 配对过程包括了配对信息交换.生成密钥和交换密钥三个 ...

  6. 认识并初步应用GitHub——C++

    好好学习,天天向上 一.这是一个根据规定的开头 GIT的地址 https://github.com/Notexcellent GIT的用户名 Notexcxllent 学号后五位 82405 博客地址 ...

  7. less 分页显示文件内容

    1.命令功能 less 是more的增强版,可以分页显示文件内容,而且less打开文件的速度要比vi,more更快.less支持搜索功能,显示行号. 2.语法格式 less  option  file ...

  8. SAP导出内表数据到excel

    DATA: EXCEL    TYPE OLE2_OBJECT,      SHEET    TYPE OLE2_OBJECT,      CELL     TYPE OLE2_OBJECT,     ...

  9. 安装tensorflow遇到:Your CPU supports instructions that this TensorFlow binary was not compiled to use

    为了提升CPU计算速度的.若你有支持cuda的GPU,则可以忽略这个问题,因为安装SSE4.1, SSE4.2, AVX, AVX2, FMA, 仅仅提升CPU的运算速度(大概有3倍). 解决方法: ...

  10. 纯前端导出Excel表格

    <html> <head> <p style="font-size: 20px;color: red;">使用a标签方式将json导出csv文件 ...