216. Combination Sum III
Question
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]]
思路
因為k是不定的, 所以無法用LOOP(迴圈). 回溯法是此類題的常用解法. 注意要新建一個list 放入結果中, 否則放入的reference 會指向原來的不斷變化的list,result.add(new ArrayList(cur));
程式碼
public class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer >> result = new ArrayList<List<Integer>>();
List<Integer> list = new ArrayList<Integer>();
if(k < 1 || k > 9 || n < 1 || n > 45)
return result;
generate(result,list,1,k,n);
return result;
}
public void generate(List <List<Integer>> result,List<Integer> list,int start,int k,int n){
//難點在於如何取得所有不重複的組合數
if(list.size() == k && n == 0){
result.add(new ArrayList(list));
return;
}
else if(list.size() >= k || n <= 0)
return;
for(int i = start;i <= 9;i++){
list.add(i);
generate(result,list,i + 1,k,n - i);
list.remove(list.size() - 1);
}
}
}
結果及分析
遞迴這種分析時間複雜度的就是看最後遍歷的次數,回溯法難就難在不好理解遍歷的順序。此次遍歷的順序是:[1,2,3]->[1,2,4]->[1,2,5]->[1,2,6]->[1,2,7]->[1,2,8]->[1,2,9]->[1,3,4]->[1,3,5]…….
二刷
思路還是一樣的,利用回溯法和dfs,code更簡單了一點。
CODE
public class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<>();
if(k == 0 || n == 0)
return res;
List<Integer> temp = new ArrayList<>();
helper(res,temp,k,n,1);
return res;
}
public void helper(List<List<Integer>> res,List<Integer> temp,int k,int n,int num){
if(temp.size() == k && n == 0){
res.add(new ArrayList<>(temp));
return;
}
else if(temp.size() > k || n < 0)
return;
for(int i = num;i <= 9;i++){
if(i > n)
return;
temp.add(i);
n -= i;
helper(res,temp,k,n,i + 1);
n += i;
temp.remove(temp.size() - 1);
}
}
}
相關推薦
Leetcode 216: Combination Sum III
urn dfs can out () inpu amp ble spa Find all possible combinations of k numbers that add up to a number n, given that only numbers from
216. Combination Sum III 組合總數三
hat [] desc cas lan script light mil number Find all possible combinations of k numbers that add up to a number n, given that only number
216 Combination Sum III 組合總和 III
com highlight ack 示例 push_back light bin 個數 i+1 找出所有可能的 k 個數,使其相加之和為 n,只允許使用數字1-9,並且每一種組合中的數字是唯一的。示例 1:輸入: k = 3, n = 7輸出:[[1,2,4]]示例 2:輸
Leetcode 216. Combination Sum III
文章作者:Tyan 部落格:noahsnail.com | CSDN | 簡書 1. Description 2. Solution class Solution { public: vector<vecto
216. Combination Sum III - Medium
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
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 c
LC 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
216. Combination Sum III
Question 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
【LeetCode-面試演算法經典-Java實現】【216-Combination Sum III (組合數的和)】
原題 Find all possible combinations of k numbers that add up to a number n, given that
[Leetcode 40]組合數和II Combination Sum II [ [ [Leetcode 216]求給定和的數集合 Combination Sum III [Leetcode 39]組合數的和Combination Sum
【題目】 Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the can
Leetcode|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 shou
LeetCode216: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 combina
[LeetCode] 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
[Swift]LeetCode216. 組合總和 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
[leetcode] Combination Sum III
簡單題,和前面兩個類似。程式碼如下: class Solution { public: vector<vector<int>> combinationSum3(int k, int n) { vector<vector&
【LeetCode】040. Combination Sum II
log bsp for ont end ati 無法 clas class 題目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinatio
【LeetCode】039. Combination Sum
set sha leet delet als unique ati solution gin 題目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), fin
[LeetCode] 40. Combination Sum II Java
array lec span mov ont integer target else list 題目: Given a collection of candidate numbers (C) and a target number (T), find all unique
[leetcode-377-Combination Sum IV]
一維數組 chang gin .html arr bin ces osi () Given an integer array with all positive numbers and no duplicates, find the number of possible c
377. Combination Sum IV
list href find logs log break desc leet == https://leetcode.com/problems/combination-sum-iv/#/description Given an integer array with a