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.
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]]
题目标签:Array, Backtracking
题目给了我们 k 和 n, 让我们找到 从1到9里的 k 个数字组合,没有重复,加起来之和等于 n。这道题目与前面两个版本的基本思路一样,利用 backtracking来做,就是有一些条件不一样而已。这题规定了只能从1 到 9 里选数字,之前两个版本的题目都是给的array;还有就是 这一题 规定了我们 数字的数量要等于 k。所以只要做一些条件的修改就可以了,具体看code。
Java Solution:
Runtime beats 45.85%
完成日期:09/06/2017
关键词:Array,Backtracking
关键点:加入数字 -> 递归 -> 去除最后一个数字 来测试所有的组合可能性
class Solution
{
public List<List<Integer>> combinationSum3(int k, int n)
{
List<List<Integer>> list = new ArrayList<>();
int len = 10; // use only numbers from 1 to 9
backtrack(list, new ArrayList<>(), n, 1, len, k); return list;
} public boolean backtrack(List<List<Integer>> list, List<Integer> tempList,
int remain, int start, int len, int size)
{
if(remain < 0 || tempList.size() > size) // if remain less than 0 or number limit exceeds
return false; // no need to continue
else if(remain == 0 && tempList.size() == size) // only add tempList into list
{ // when remain = 0 and number limit size matches
list.add(new ArrayList<>(tempList));
return false;
}
else
{
for(int i=start; i<len; i++)
{
boolean flag;
tempList.add(i);
flag = backtrack(list, tempList, remain - i, i+1, len, size); // i + 1 because we cannot use same number more than once
tempList.remove(tempList.size() - 1); if(!flag) // if find a tempList answer or fail, no need to continue that loop
break; // because 1~9 is sorted and unique, the rest numbers are greater, they'll fail
} return true;
}
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 216. Combination Sum III (组合的和之三)的更多相关文章
- [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 ...
- 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 ...
- Leetcode 216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 216 Combination Sum III 组合总和 III
找出所有可能的 k 个数,使其相加之和为 n,只允许使用数字1-9,并且每一种组合中的数字是唯一的.示例 1:输入: k = 3, n = 7输出:[[1,2,4]]示例 2:输入: k = 3, n ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 【LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- 【刷题-LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- python基础之元组,集合
一.元组 为何要有元组,存放多个值,元组不可变,更多的是用来做查询 t=(,[,],,)) #t=tuple((,[,],,))) print(type(t)) 元组可以作为字典的key d={(,, ...
- 编译安装Nginx到Linux
之前安装的H2O不知道为啥,总是崩溃,换Nginx了下载包:http://nginx.org/download/ 配置:./configure --prefix=/usr/local/nginx -- ...
- (转)添加PROPAGATION_REQUIRES_NEW 事务没有产生作用
最近在做事务添加时 发现自己的事务没有新建,上网查到 仅用作收藏. 其二 注意 事务的注解 应该在 内层的事务上面 一.描述 Spring遇到嵌套事务时,当被嵌套的事务被定义为" ...
- ①【javascript设计到的技术点】
一.dom操作: document.getElementById() document.getElementsByTagName() 二.事件操作: dom2级事件 主流浏览器 addEventLis ...
- MySql join on 和 where
原文:http://www.cnblogs.com/Jessy/p/3525419.html left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录. right join : ...
- 如何解决conda install:command not found问题
每次运行conda相关代码之前先做一遍source ~/.bashrc.即可
- 【转】Keberos认证原理
前几天在给人解释Windows是如何通过Kerberos进行Authentication的时候,讲了半天也别把那位老兄讲明白,还差点把自己给绕进去.后来想想原因有以下两点:对于一个没有完全不了解Ker ...
- TOP命令详解(负载情况)
简介 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按 ...
- Codeforces 858A. k-rounding 数论
题目: 题意:输入n和k,找到一个最小的数,满足末尾有至少k个0和是n的倍数. 最小的情况 ans = n,最大的情况 ans = n*pow(10,k). 令 k = pow(10,k); 我们发现 ...
- 原创:TSP问题解决方案-----禁忌搜索算法C实现
本文着重于算法的实现,对于理论部分可自行查看有关资料可以简略参考该博文:http://blog.csdn.net/u013007900/article/details/50379135 本文代码部分基 ...