解题思路:recursion
Java Code:
public List<List<Integer>> combinationSum(int[] candidates, int target) { Arrays.sort(candidates); List> res = new ArrayList
>(); List
solution = new ArrayList (); combination(candidates, 0, target, res, solution); return res; } public void combination(int[] candidates, int startIndex, int target, List > res, List
solution){ if(target == 0){ List tmp = new ArrayList (solution); res.add(tmp); return; } for(int i = startIndex; i < candidates.length; i++){ if(candidates[i] <= target){ solution.add(candidates[i]); combination(candidates, i, target - candidates[i], res, solution); solution.remove(solution.size() - 1); }else break; } }
No comments:
Post a Comment