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基础之字典、赋值补充
字典常用操作: 存/取info_dic={'name':'egon','age':18,'sex':'male'} print(info_dic['name11111111']) print(info ...
- 纳税服务系统【自动受理,Quartz任务调度】
需求 回到我们的需求: 自动投诉受理:在每个月月底最后一天对本月之前的投诉进行自动处理:将投诉信息的状态改为 已失效.在后台管理中不能对该类型投诉进行回复. 这个需求需求我们要怎么弄呢????要在每个 ...
- mysql数据库-中文乱码问题解决方案
来自:http://www.2cto.com/database/201108/101151.html MySQL会出现中文乱码的原因不外乎下列几点: .server本身设定问题,例如还停留在latin ...
- WEB项目的部署结构
tomcat/webapps目录是用来存放Java项目的.每一个文件夹都是一个项目,默认这个目录下已经有了四个项目,都是tomcat自带的. 其中ROOT就是我们测试Tomcat时访问的Tomcat的 ...
- HDU 6092`Rikka with Subset 01背包变形
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 使用cocos2d脚本生成lua绑定
这几天要老大要求把DragonBones移到cocos2dx 3.0 里边,并且绑定lua使用接口.因为刚学lua,使用的引擎也刚从2.2改为3.0,各种不熟悉,折腾了好几天才弄完,有空了总结一下 这 ...
- C语言bitmap的使用技巧
bitmap是一种以位的状态来表示某种特性的状态的一种操作方式,类似嵌入式中的寄存器操作,在表示某种特性enable/disable的时候很适用且占用的内存空间也很小 比如:做过交换机或者企业网管,路 ...
- node.js express mvc轻量级框架实践
本文记录的是笔者最近抽私下时间给朋友做的一个时时彩自动下注系统,比较简单,主要也是为了学习一下node.js. 其实逻辑没什么可以深谈的,主要是想说说这套代码结构.结构如下图: js的代码比较难以维护 ...
- python pyinstaller打包exe暗坑1
环境 python2.7.9 win-xp 今天打包了一个小脚本,结果打开报错
- 一张图讲解对象锁和关键字synchronized修饰方法
每个对象在出生的时候就有一把钥匙(监视器),那么被synchronized 修饰的方法相当于给方法加了一个锁,这个方法就可以进行同步,在多线程的时候,不会出现线程安全问题. 下面通过一张图片进行讲解: ...