【题目】

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Note:

    • All numbers will be positive integers.
    • The solution set must not contain duplicate combinations.

Example 1:

Input: k = 3, n = 7
Output: [[1,2,4]]

Example 2:

Input: k = 3, n = 9
Output: [[1,2,6], [1,3,5], [2,3,4]]

【思路】

回溯,有模板。

适用范围:需要返回点的集合,形如List<List<Integer>>。

思路:新建fun函数迭代,从一个点flag开始迭代到边界点。

List<List<Integer>> ans=new ArrayList<>();

List<Integer> tmp=new ArrayList<>();

对于ans:当tmp满足题目要求,把tmp中的答案作为集合加入到ans中。

对于tmp:tmp中临时存储每次迭代的答案集合,每完成一次回溯,tmp.remove(tmp.size()-1)保证新一次循环时,tmp为空

for循环flag到end,flag是已经遍历到的数据,end是遍历的终点(目标)。

反复迭代fun(ans,tmp,i+1,k,n-i);//距离期望还差n-i

【相关题目】

1、[Leetcode 78]求子集 Subset https://www.cnblogs.com/inku/p/9976049.html

2、[Leetcode 90]求含有重复数的子集 Subset II https://www.cnblogs.com/inku/p/9976099.html

3、讲解在这: [Leetcode 216]求给定和的数集合 Combination Sum III

4、[Leetcode 39]组合数的和Combination Sum

【代码】

class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> ans=new ArrayList<>();
List<Integer> tmp=new ArrayList<>();
fun(ans,tmp,1,k,n);
return ans;
}
public void fun(List<List<Integer>> ans,List<Integer> tmp,int flag,int k,int n){
if(tmp.size()==k&&n==0)
ans.add(new ArrayList<Integer>(tmp));
for(int i=flag;i<=9;i++){
tmp.add(i);
fun(ans,tmp,i+1,k,n-i);
tmp.remove(tmp.size()-1);
}
}
}

[Leetcode 216]求给定和的数集合 Combination Sum III的更多相关文章

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

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

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

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

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

  4. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

  6. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  7. LeetCode Combination Sum III

    原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...

  8. LeetCode 216. Combination Sum III (组合的和之三)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  9. LeetCode 216. 组合总和 III(Combination Sum III)

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

随机推荐

  1. topcoder srm 615 div1

    problem1 link 对于数字$x$,检验每个满足$x=y*2^{t}$的$y$能否变成$x$即可. problem2 link 如果起点到终点有一条长度为$L$的路径,那么就存在长度为$L+k ...

  2. 64位 windows2008 R2 上安装32位oracle 10g 的方法

    首先,我们要解除oracle安装的windows版本检测1.编辑安装包内文件  database\stage\prereq\db\refhost.xml 在 <OPERATING_SYSTEM& ...

  3. SpringBoot和druid数据源集成Jpa

    1.pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  4. SQL Server監控与診斷

    僅為記錄工作中遇到的問題. 1. 字符串截斷: SQL server里很多job用於運行DTS,經常會收到系統出錯警報,如: ...String ) The statement has been te ...

  5. Linux环境——MySQL安装及配置(8.0版本)

    虚拟机环境是Linux  Red Hat Enterprlse Linux (64位),本次安装的是Mysql 8.0版本. 由于有经验了,所以又弄了台虚拟机练手,承接上一篇博客(https://ww ...

  6. 记 Swagger 2

    Maven坐标: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox- ...

  7. CSS scroll-behavior和JS scrollIntoView让页面滚动平滑

    转自 https://www.zhangxinxu.com/wordpress/2018/10/scroll-behavior-scrollintoview-%E5%B9%B3%E6%BB%91%E6 ...

  8. ArcGIS Pro开发Web3D应用(4)——Portal发布三维没有“发布”选项解决

    如题,将三维场景包.slpk上传并发布,未看到发布选项. 问题核心就在于环境没配置好,主要有2点: 1.验证联合托管状态 需要将托管服务器与Portal进行联合,并进行验证,此情况需要在WebAdap ...

  9. Vue组件通信

    单向数据流通信 单向数据流通信是指父组件传递数据给子组件,子组件是不可以修改该数据的(可以改,但会警告) 父组件通过自定义属性传递数据给子组件,子组件使用props接收 如果想修改数据,子组件需要使用 ...

  10. [JavaScript] 表单验证不通过不提交的JS写法

    主要是本世纪初的写法.<script> function validateForm(f) { if (f.name.value == "") { alert(" ...