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 (组合的和之三)的更多相关文章

  1. [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 ...

  2. 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 ...

  3. Leetcode 216. Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. 216 Combination Sum III 组合总和 III

    找出所有可能的 k 个数,使其相加之和为 n,只允许使用数字1-9,并且每一种组合中的数字是唯一的.示例 1:输入: k = 3, n = 7输出:[[1,2,4]]示例 2:输入: k = 3, n ...

  5. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  6. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  7. 【LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  8. 【刷题-LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  9. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. sqlserver2012 密码过期问题

    昨天登录系统突然连不上数据库了看了看报错内容提示是sqlserver的用户密码过期,那么就简单记录下操作,方便孩子后解决 (1)首先打开sql Management Studio 2012 顺便提一下 ...

  2. Linux第二篇【系统环境、常用命令、SSH连接、安装开发环境】

    系统环境 我们知道Windows的出色就在于它的图形界面那一块,而Linux对图形界面的支持并不是那么友好-其实我们在Windows下对图形界面进行的操作都是得装换成命令的方式的! 当然了,我们在Ub ...

  3. jmeter测试HTTP请求

    HTTP超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.(详情参考看一下百科) HTTP发送请求有GE ...

  4. PuTsangTo-单撸游戏开发02 测试场景与单轴移动

    且不说立项与设计阶段的工作量,一个完整的游戏在开发阶段设计的职责范围也是很广,还有个大问题就是PuTsangTo项目也是本人在边学边做,截止目前还是满满的无从下手的感觉,一方面是技能与经验不足,另一方 ...

  5. MMORPG战斗系统随笔(一)

    前言 很久没有更新博客,中间迁移过一次博客,后来一直忙于项目的开发,忙的晚上回去没时间写博客,周日又要自我调整一下,所以空闲了很久没有继续写博客.最近终于慢慢放慢节奏,项目也快上线了,可以有空写一些个 ...

  6. 在JavaScript中使用json.js:访问JSON编码的某个值

    演示: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  7. asp.net core后台系统登录的快速构建

    登录流程图 示例预览 构建步骤 当然,你也可以直接之前前往coding仓库查看源码,要是发现bug记得提醒我啊~ LoginDemo地址 1. 首先你得有一个项目 2. 然后你需要一个登录页面 完整L ...

  8. IntelliJ IDEA 14.1.4设置关闭自动保存和标志改动文件为星号?

    一.设置取消默认的自动保存: 二.设置文件内容改动后为文件标题头部添加*号:

  9. Vue 爬坑之路(六)—— 使用 Vuex + axios 发送请求

    Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource 目前主流的 Vue 项目,都选择 axios ...

  10. CodeSmith(C#)简单示例及相关小知识

    // 本文将介绍CodeSmith与数据库进行交互生成相应的存储过程,本例使用的数据库为SQL Server 2000. // 在与数据库进行交互时,我们使用到了一个CodeSmith自带的组件Sch ...