码迷,mamicode.com
首页 > 其他好文 > 详细

Jan 16 - Combination Sum; Array; BackTracking;

时间:2016-01-17 13:36:31      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

添加两个List:List<Integer> listSum 存储对应组合的和;List<List<Integer>> list 存储可能的组合

resultList存储和==target的组合

代码:

public class Solution {
    public List<Integer> listSum;
    public List<List<Integer>> list;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        listSum = new ArrayList<>();
        list = new ArrayList<>();
        List<List<Integer>> resultList = new ArrayList<>();
        int len = candidates.length;
        if(len == 0) return resultList;
        for(int i = 0; i < len; i++){
            int digit = candidates[i];
            if(digit > target) break;
            resultList = traverseArray(resultList, digit, target);
        }
        return resultList;
        
    }
    
    public List<List<Integer>> traverseArray(List<List<Integer>> resultList, int digit, int target){
        List<Integer> sum = new ArrayList<>();
        List<List<Integer>> list_combination = new ArrayList<>();
        int n_digit = digit;
        int count = 0;
        while(n_digit < target){
            count++;
            List<Integer> combination = new ArrayList<>();
            for(int i = 0; i < count; i++) combination.add(digit);
            list_combination.add(combination);
            sum.add(n_digit);
            n_digit += digit;
        }
        if(n_digit == target){
            List<Integer> oneDigitCombination = new ArrayList<>();
            for(int i = 1; i <= count+1; i++) oneDigitCombination.add(digit);
            resultList.add(oneDigitCombination);
        }
        
        if(listSum.size() == 0){
            listSum = sum;
            list = list_combination;
            return resultList;
        }
        int list_sum_length = listSum.size();
        for(int j = 0; j < list_sum_length; j++){
            List<Integer> c1 = list.get(j);
            
            for(int k = 0; k < count; k++){
                int t = listSum.get(j) + sum.get(k);
                List<Integer> c2 = list_combination.get(k);
                if(t < target){
                    listSum.add(t);
                    List<Integer> l = new ArrayList<>();
                    for(int i = 0; i < c1.size(); i++) l.add(c1.get(i));
                    for(int i = 0; i < c2.size(); i++) l.add(c2.get(i));
                    list.add(l);
                }
                if(t == target){
                    List<Integer> l = new ArrayList<>();
                    for(int i = 0; i < c1.size(); i++) l.add(c1.get(i));
                    for(int i = 0; i < c2.size(); i++) l.add(c2.get(i));
                    resultList.add(l);
                }
            }
        }
        for(int j = 0; j < count; j++){
            listSum.add(sum.get(j));
            list.add(list_combination.get(j));
        }
        return resultList;
        
        
    }
}

  https://leetcode.com/discuss/75647/4ms-java-solution

Jan 16 - Combination Sum; Array; BackTracking;

标签:

原文地址:http://www.cnblogs.com/5683yue/p/5137099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!