Given a set of candidate numbers (C)(without duplicates)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 target7,
A solution set is:
publicList<List<Integer>>combinationSum(int[] candidates,int target) {List<List<Integer>> res =newArrayList<>();if (candidates ==null||candidates.length==0) {return res; }ArrayList<Integer> tmp =newArrayList<>();dfsHelper(tmp, res, target,0, candidates);return res;}privatevoiddfsHelper(ArrayList<Integer> tmp,List<List<Integer>> res,int target,int start,int[] candidates) {if (target ==0) {res.add(newArrayList<>(tmp));return; }// i begins from start, because we pick previous numbers already. // now need to pick following numbersfor (int i = start; i <candidates.length; i++) {// if bigger than target, target will be neg, so we skip themif (candidates[i] > target) {continue;// not break, because the set is not ordered }tmp.add(candidates[i]);// recur from i, bucause same number can use more than oncedfsHelper(tmp, res, target - candidates[i], i, candidates);tmp.remove(tmp.size() -1); }}