每日一题 2019 - 03 - 26
题目:
Given a set of candidate numbers (candidates
) (without duplicates) and a target number (target
), find all unique combinations in candidates
where the candidate numbers sums to target
.
The same repeated number may be chosen from candidates
unlimited number of times.
Note:
- All numbers (including
target
) will be positive integers. - The solution set must not contain duplicate combinations.
Example 1:
1 | Input: candidates = [2,3,6,7], target = 7, |
Example 2:
1 | Input: candidates = [2,3,5], target = 8, |
解法:
这个题让我们在给定的数组里面找到与 target
值相等的数字和的序列,简单来说就是递归回溯的思想,从数组里面一个数字出发,深层次遍历所有的分支,也即:
1 | if(sum == target) { |
这个题的解题关键是把回溯的条件要设置清楚,以及要把迭代过程中的暂存 vector
弹出
代码:
1 | class Solution { |