【题目】

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. 使用 pm2 优雅的部署 node 程序

    使用 pm2 优雅的部署 node 程序 # 启动并监控名字为 XXX 的 npm run start:dev 命令 pm2 start npm --watch --name XXX -- run s ...

  2. Vue-admin工作整理(九): 状态管理Vuex-state和getters

    前提:通过项目结构个人设置创建的项目 store文件下actions.js.mutations.js.state.js都是根级别的状态管理,引用入口是通过index.js来实现,整个Vuex处理逻辑为 ...

  3. 北京AI外包团队 祝大家2019事业有事,大吉大利!

    未来已来,以人工智能为核心的科技力量,在重新塑造着我们生活的环境.这种重新塑造的现象如此之明显,力量如此强大,以至于越来越多的人在讨论,我们面临着新一轮的工业革命.而且现在我们面临的这次新的科技力量, ...

  4. 让小乌龟可以唱歌——对Python turtle进行拓展

    在Scratch中,小猫是可以唱歌的,而且Scratch的声音木块有着丰富的功能,在这方面Python turtle略有欠缺,今天我们就来完善一下. Python声音模块 Python处理声音的模块很 ...

  5. 为wordpress后台登陆添加算术验证码

    对于新建站(个人博客-柠檬https://ninmong.com)的站长来说提高后台的安全性,是一件非常重要的事,添加验证可以起到很好的效果,废话少说,贴代码 //后台登陆数学验证码 function ...

  6. js if判断示例

    ){ ){ console.log("%0 pass") }else{ $(,v,function() { fla=; }); } }){ ){ console.log(" ...

  7. jmeter需要学习的其他点

    jmeter知识点访问地址参数化http请求默认值远程连接非GUI运行测试命令jmeter参数设置jmeter常用脚本开发 Beanshell sampleDebug SampleFtp请求java请 ...

  8. 网站性能测试工具 webbench 的安装和使用

    1.webbench的下载和安装 wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz sudo tar xvf we ...

  9. C#实现基于ffmpeg加虹软的人脸识别demo及开发分享

    对开发库的C#封装,屏蔽使用细节,可以快速安全的调用人脸识别相关API.具体见github地址.新增对.NET Core的支持,在Linux(Ubuntu下)测试通过.具体的使用例子和Demo详解,参 ...

  10. spring boot2.0(二 ) lettcute访问redis

    前言 此处已经省略redis的安装,请自行百度查找redis的服务端安装过程. 1.pom文件配置: <project xmlns="http://maven.apache.org/P ...