LeetCode – Combination Sum (Java)
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]
Java Solution
The first impression of this problem should be depth-first search(DFS). To solve DFS problem, recursion is a normal implementation.
The following example shows how DFS works:
public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> result = new ArrayList<>(); List<Integer> temp = new ArrayList<>(); helper(candidates, 0, target, 0, temp, result); return result; } private void helper(int[] candidates, int start, int target, int sum, List<Integer> list, List<List<Integer>> result){ if(sum>target){ return; } if(sum==target){ result.add(new ArrayList<>(list)); return; } for(int i=start; i<candidates.length; i++){ list.add(candidates[i]); helper(candidates, i, target, sum+candidates[i], list, result); list.remove(list.size()-1); } } |
<pre><code> String foo = "bar"; </code></pre>
-
TISHANT CHANDRAKAR
-
Derek
-
pjpal
-
Александр Ефремов
-
Didier
-
gowtham Bk
-
ryanlr
-
divya
-
Surbhi Motghare
-
Dexter
-
Jeffrey Ni
-
Lean Lee