[Leetcode 216]求给定和的数集合 Combination Sum III
【题目】
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的更多相关文章
- Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)
Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...
- [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 ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 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 ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
- LeetCode 216. Combination Sum III (组合的和之三)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode 216. 组合总和 III(Combination Sum III)
题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合. 示例 1: 输入 ...
随机推荐
- 主成分分析 SPSS、python实例分析
今天,在西瓜书上看到了主成分分析法,之前建模有接触过但是理解不够深刻,今天再次和这一位老朋友聊聊. 主成分分析(Principal Component Analysis,PCA), 是一种统计方法.通 ...
- Java之冒泡算法实现
算法说明:给一列数组排序,当前一个元素大于后一个元素则交换这两个元素的顺序,直到最大的数字移动到最右边,以剩下n-1元素组成的数组当做最新数组,重复交换过程,直到这个数组全部处理完毕.传的参数一个是数 ...
- 清除本地SVN信息
C:\Documents and Settings\yangxf\Application Data\Subversion\auth 这个目录下删除svn文件夹即可
- P4717 【模板】快速沃尔什变换
思路 FWT的模板 FWT解决这样的卷积 \[ C_k=\sum_{i\otimes j=k} A_iB_j \] \(\otimes\)可能是and,or,xor等位运算 几个式子 FWTand: ...
- Linux 文件内容查看(cat、tac、nl 、more 、less、head、tail )
Linux系统中使用以下命令来查看文件的内容: cat: 由第一行开始显示文件内容tac :从最后一行开始显示,可以看出 tac 是 cat 的倒著写!nl: 显示的时候,顺道输出行号!more ...
- hyperledger fabric 架设命令
单节点架设 Order 网络: cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli/ rm -rf channel-artifacts ...
- CSRF、XSS、clickjacking、SQL 的攻击与防御
CSRF攻击 原理: 跨站请求伪造.是一种挟制用户在当前已登录的Web应用程序上执行非本意的操作的攻击方法. 网站通过cookie来实现登录功能.而cookie只要存在浏览器中,那么浏览器在访问含有这 ...
- docker安装nginx实例
1.拉取nginx镜像: docker pull nginx 2.查看本地镜像文件: docker images 3.创建挂载目录: mkdir -p /docker_data/nginx/{con ...
- ZJOI-2017 R2 游记
来说说考试(之前的事明天再补): 开始看了一遍所有题目,感觉第二题最可做的样子(ZJOI R1树状数组,HNOI splay 你们西方什么题我还没见过,淦!),大概感觉了一下所有题. T1:k=1直接 ...
- JedisPubSub
MsgListener extends JedisPubSub notify-keyspace-events "KEA"