Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]
這道題看過好幾次最開始都沒看出怎麼做就一直留著,結果今天在做Subsets 這道題時找到了靈感,使用深度優先的方法搜索,並且用一個vector記錄向量,找到合適的向量時即將它保存在結果中,並進行回溯操作。這道題相比於Subsets中使用回溯法而言更麻煩一些。以後需要注意這種解題方法,當要求解的結果是一系列向量的集合時使用dfs搜索記錄路徑這種方法。對於輸入candidates=[1,2] ,target=3,遍歷的方向如圖:
runtime:28ms<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;">
class Solution {
public:
vector