1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> list = new ArrayList<List<Integer>>(); List<Integer> node = new ArrayList<Integer>(); dfs(candidates,target,0,node,list); return list; } public void dfs(int[] candidates,int target,int i,List<Integer>node,List<List<Integer>>list) { int length=candidates.length; if(i==length)return; if(target==0) { list.add(new ArrayList<Integer>(node)); return; } dfs(candidates,target,i+1,node,list); if(target-candidates[i]>=0) { node.add(candidates[i]); target-=candidates[i]; dfs(candidates,target,i,node,list); node.remove(node.size()-1); } } }
|