题目地址:
题目分析:采用递归方式,先将数组排序,依次遍历数组中元素,如果target比第i个元素大则加入到结果集中,如果比第i个元素小返回;如果target为0,将组合加入到返回值中。
题目解答:
import java.util.ArrayList;import java.util.Arrays;public class Solution { public ArrayList> combinationSum(int[] candidates, int target) { ArrayList curr = new ArrayList (); ArrayList > ret = new ArrayList >(); if(candidates == null || candidates.length == 0 || target == 0){ return ret; } Arrays.sort(candidates); combinationSum(candidates,0,target,curr,ret); return ret; } public void combinationSum(int[] candidates,int start,int target,ArrayList curr,ArrayList > ret){ if(target == 0){ ArrayList temp = new ArrayList (curr); ret.add(temp); return; } for(int i = start;i