easy understanding java solution with detailed explanation in my blog(better for Chinese people,backtracking)


  • 0
    S

    you can visit my blog:http://blog.csdn.net/versencoder/article/details/52071930
    my solution:
    public class Solution {
    List<List<Integer>> result=new ArrayList<List<Integer>>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
    Arrays.sort(candidates);
    List<Integer> list=new ArrayList<Integer>();
    backtracking(candidates,target,0,list);
    return result;
    }
    public void backtracking(int[] candidates,int target,int start,List<Integer> list){

            if(target<0)    return;
            else if(target==0){
                
                result.add(new ArrayList<>(list));
                
            }else{
                for(int i=start;i<candidates.length;i++){
                    list.add(candidates[i]);
                    backtracking(candidates,target-candidates[i],i,list);
                    list.remove(list.size()-1);
                }
            }
        
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.