Difficult:Medium
題目
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.
The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input.
翻譯
給定一個由不同整數候選者組成的數組和一個目標整數目標,返回一個包含所有唯一候選者組合的列表,其中所選數字總和為目標。您可以按任何順序返回組合。
可以從候選人中無限次選擇相同的數字。如果至少一個所選數字的頻率不同,則兩個組合是唯一的。
對於給定的輸入,保證總和為目標的唯一組合的數量少於 150 個組合。
範例
Example 1:
1 | Input: candidates = [2,3,6,7], target = 7 |
Example 2:
1 | Input: candidates = [2,3,5], target = 8 |
Example 3:
1 | Input: candidates = [2], target = 1 |
解題思路
1.將題目分成小問題
2.利用遞迴求解
Solution
Code 1 :
1 | var combinationSum = function(candidates, target) { |