[LeetCode][JavaScript]Combination Sum III

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.

Ensure that numbers within the set are sorted in ascending order.

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]]

https://leetcode.com/problems/combination-sum-iii/




我做到现在AC最快的题目,真的不到1分钟,还是上两题的变通:

http://www.cnblogs.com/Liok3187/p/4526863.html

http://www.cnblogs.com/Liok3187/p/4532064.html

把第二题的代码copy过来,删掉visited,candidates换成一到9,最后加上个数的限制,就好了。

真会玩啊,一个类型翻来覆去,很欢迎Combination Sum IV

 1 /**
 2  * @param {number} k
 3  * @param {number} n
 4  * @return {number[][]}
 5  */
 6 var combinationSum3 = function(k, n) {
 7     var res = [];
 8     var candidates = [1,2,3,4,5,6,7,8,9];
 9     candidates.sort(sorting);
10     bfs(0, -1, []);
11     return res;
12
13     function bfs(sum, index, tmp){
14         var newTmp = null;
15         if(sum === n && tmp.length === k){
16             newTmp = tmp.concat();
17             res.push(newTmp);
18             return;
19         }else if(tmp.length > k || sum > n || index + 1 >= candidates.length){
20             return; //pruning
21         }
22
23         for(var i = index + 1; i < candidates.length; i++){
24             newTmp = tmp.concat();
25             newTmp.push(candidates[i]);
26             bfs(sum + candidates[i], i, newTmp);
27         }
28     }
29     function sorting(a, b){
30         if(a > b){
31             return 1;
32         }else if(a < b){
33             return -1;
34         }else{
35             return 0;
36         }
37     }
38 };
时间: 2024-10-12 22:05:20

[LeetCode][JavaScript]Combination Sum III的相关文章

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 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

[LeetCode][JavaScript]Combination Sum II

Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including t

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

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

【leetcode】Combination Sum III(middle)

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. Ensure that numbers within the set are sorted in ascending order. Example 1

leetcode 216. Combination Sum III 求和III ---------- java

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

leetCode(31):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. Ensure that numbers within the set are sorted in ascending order. Example 1

LeetCode 216. Combination Sum III(DFS)

题目 题意:从1-9中选出k个数之和等于n,这个k个数不能有相同的,输出所有可能的k个数字的集合,结果也不能重复 题解:暴搜,从n开始,每次减去1-9中的某个数字,然后继续递归.要注意剪枝,比如1-9中的数字大于n/k的是不可能存在答案中的,如果n 的值小于sum[k]也是不会有答案的.sum[k]表示k个数字最小和的组合.当然k>=10的时候,也是没有答案的. class Solution { public: vector<vector<int>> res; vector&

Leetcode题解(4):L216/Combination Sum III

L216: 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. Ensure that numbers within the set are sorted in