39. Combination Sum
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.
- 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] ]
思路:
可以重复使用元素。那么每次调用从当前位置开始,如果当前位置元素小于目标元素,加入path,再次从当前位置调用;每个call中都要遍历后面的可能位置一遍。因此排序+剪枝。
Read full article from Leetcode 39 Combination Sum & 40 Combination Sum II & 216 Combination Sum III & 377 Combination V - fantasia的博客 - CSDN博客
No comments:
Post a Comment