【题目】

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. harbor pull 失败

    STep1: 显示下面的错误 Error response from daemon: unknown: The image is not signed in Notary Step2: 这儿暂时不要选 ...

  2. dom节点相关问题

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  3. HTML与CSS的一些知识(二)

    续: 5.表单标签<form></form> 用于收集用户信息,统一提交到服务器 一般用input标签收集,再用提交按钮提交:input标签根据type属性值不同有不同的类型: ...

  4. (一)为什么要UML

    1 建模的意义 模型是对于现实的简化,建模是为了更好的理解系统 模型帮助我们按照实际情况或需求对系统可视化 模型允许我们详细说明系统的构造,行为 模型给出一个构造系统的模板 模型对我们做出的决策进行文 ...

  5. C#演示如何使用 XML 将源码编入文档

    工作闲暇时间,将做工程过程中常用的代码段记录起来,下面的代码是关于C#演示如何使用 XML 将编入文档的代码,希望对大伙有较大帮助. using System; public class SomeCl ...

  6. kubernetes1.13.5安装部署

    Kubernetes 一.    Kubernetes介绍 各节点所需组件 Master:docker,kubeadm,kubelet, 组件 版本 备注 Kubelet 1.13.5 组件 Kube ...

  7. 【转】 glibc detected *** corrupted double-linked list:错误的原因有如下三种可能

    一个多线程的大程序运行的时候崩掉了,屏幕上打出这个:   *** glibc detected *** corrupted double-linked list: 0xb78381d8 *** 三个原 ...

  8. WPF界面假死

    首先要检查那些滥用 Timer.Dispacher Timer 或者滥用什么“线程+死循环+阻塞”轮询的代码. 这种是编程大忌,有些人不会设计事件驱动程序,而是滥用轮询. 若是:触发事件后的假死,搜W ...

  9. linux shell实战之知识体系

    1.认识GUN/bash shell 梳理清楚硬件,内核及模块,shell之间的关系:熟悉GUN的bash以及bash shell的功能:学习shell的通配符 2.shell 的变量 变量的设置,取 ...

  10. JavaJDK8新特性相关知识整理

    1.新增接口默认方法和接口静态方法     接口默认方法用default关键字修饰,与抽象方法不同之处在于抽象方法必须要求实现,而默认方法没有这个要求,默认方法本身已经有具体的实现,所有的接口实现类将 ...