import java.util.Arrays;
import java.util.List;
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
fun(candidates, 0, 0, new ArrayList<Integer>(), target, result);
return result;
}
public static void fun(int[] candidate,int start, int cur, ArrayList<Integer> list, int target, List<List<Integer>> result) {
if(cur>target) return;
else if(cur==target) {
result.add(list);
}
for(int i=start;i<candidate.length;i++){
ArrayList<Integer> temp = new ArrayList<Integer>(list);
temp.add(candidate[i]);
fun(candidate, i, cur+candidate[i], temp, target, result);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] candidate = {2,3,6,7};
List<List<Integer>> result =new Solution().combinationSum(candidate, 7);
for(int i=0;i<result.size();i++){
for(int j=0;j<result.get(i).size();j++){
System.out.print(result.get(i).get(j)+" ");
}
System.out.println();
}
}
}
leetcode39 Combination Sum
最新推荐文章于 2016-02-26 15:19:57 发布