/ * *
The title
Given an array of no duplicate elements and a target number, find all combinations of the candidates that make the numeric sum target.
The numbers in the candidates can be selected repeatedly without limit.
Description:
All numbers (including target) are positive integers. The solution set cannot contain repeated combinations. Example 1:
Enter: candidates = [2,3,6,7], target = 7, the solution set is [[7], [2,2,3]
Input: candidates =,3,5 [2], target = 8, solving sets: [,2,2,2 [2], filling [2], [3, 5]]
Tip:
1 <= candidates.length <= 30 1 <= candidates.length <= 30 1 <= candidates.length <= 50 1 <= target <= 500
Source: LeetCode link: leetcode-cn.com/problems/co… Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.
The test code
Print (combinationSum,3,6,7 [2], (7))
notes
My feeling of this problem is that the method of exhaustion as long as less than the target may be considered, and then is to exhaust the past step by step, pay attention to the middle of the pruning in advance
The code address
Github.com/zmfflying/Z… * /
The problem solving code
import Foundation func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { guard candidates.count > 0 else { return [[]] } var res: [[Int]] = [[Int]]() var path: [Int] = [Int]() let count = candidates.count func help(sum: Int, curIndex: If sum > target {return} if sum == target {res.append(path) return} // For index in curIndex... If new > target {continue} <count {let num = request [index] let new = sum + num Path.append (num) // If index = curIndex, help(sum: new, curIndex: index) path.removeLast() } } help(sum: 0, curIndex: 0) return res }Copy the code