Created
August 23, 2016 21:35
-
-
Save leearmee35/979ec58abc0840f7c1883485b48be542 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
private ArrayList<List<Integer>> res = new ArrayList<List<Integer>>(); | |
private ArrayList<Integer> sumr = new ArrayList<Integer>(); | |
public List<List<Integer>> combinationSum(int[] candidates, int target) { | |
helper(candidates, target, sumr,0); | |
return res; | |
} | |
public void helper(int[] list, int target, ArrayList<Integer> sumr, int sum){ | |
if(sum==target){ | |
ArrayList<Integer> a = new ArrayList<Integer>(sumr); | |
res.add(a); | |
return; | |
} | |
if(sum>target){ | |
return; | |
} | |
for(int i=0;i<list.length;i++){ | |
sumr.add(list[i]); | |
sum +=list[i]; | |
int nl[] = Arrays.copyOfRange(list, i, list.length); | |
helper(nl, target, sumr, sum); | |
sum-=list[i]; | |
sumr.remove(sumr.size()-1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment