【题目】

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. python开发环境配置和python源码打包生成exe可执行文件

    Windows下开发环境准备 1.分别安装:python2和python32.安装Python的集成工具:Anaconda3.安装Pycharm Pycharm设置 设置: File->Sett ...

  2. gulp插件实现压缩一个文件夹下不同目录下的js文件(支持es6)

    gulp-uglify:压缩js大小,只支持es5 安装: cnpm: cnpm i gulp-uglify -D yarn: yarn add gulp-uglify -D 使用: 代码实现1:压缩 ...

  3. c# 集合去重并筛选

    (from context in retDataList group context by context.ResourceId into g select g.OrderByDescending(b ...

  4. 基于SVD的图像压缩

    算法简介 算法实现 我只是简单处理了一下图像的灰度值,如果要处理RGB值的话,就需要分别进行SVD分解,最后再合起来即可. import numpy as np from PIL import Ima ...

  5. 整合SSH框架实现简单登录

    SSH整合的大体结构:我们将Struts2和hibernate交给spring来管理 创建好web项目之后,首先当然是先引入需要的pom节点,需要的pom的节点可以在Maven官方仓库中下载https ...

  6. curl的POST请求,封装方法

    //POST请求//参数1是请求的url//参数2是发送的数据的数组//参数3是其他POST选项public static function POST($url, array $post = arra ...

  7. Git的基本使用教程

    http://www.cnblogs.com/tugenhua0707/p/4050072.html 上传本地文件仓库到远程仓库大致步骤: 1.在安装完git时,先创建本地的一个仓库(新建一个文件夹) ...

  8. 关于Oracle单行函数的讲解

    单行函数:对单个数值进行操作,并返回一个值. 分类:1.字符函数    1)concat(a,b) 拼接a,b两个字符串数据    2)initcap(x) 将每个单词x首字母大写     3)low ...

  9. 内置函数&匿名函数

    1.内置函数     Built-in Functions     abs() dict() help() min() setattr() all() dir() hex() next() slice ...

  10. jieba库的使用和好玩的词云

    1.jieba库基本介绍 (1).jieba库概述 jieba是优秀的中文分词第三方库 - 中文文本需要通过分词获得单个的词语         - jieba是优秀的中文分词第三方库,需要额外安装 - ...