Jan 16 - Combination Sum; Array; BackTracking;

添加两个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

时间: 2024-08-09 16:00:38

Jan 16 - Combination Sum; Array; BackTracking;的相关文章

[LeetCode] Backtracking Template for (Subsets, Permutations, and Combination Sum)

根据issac3 用Java总结了backtracking template, 我用他的方法改成了Python. 以下为template. 1 def backtrack(ans, temp, nums, start): # 可能有start, 也可能没有 2 if len(temp) == len(nums): 3 ans.append(temp) 4 else: 5 for i in range(start, len(nums)): 6 if nums[i] not in temp: 7 b

[array] leetcode - 39. Combination Sum - Medium

leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from

[array] leetcode - 40. Combination Sum II - Medium

leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combinat

【leetcode】Combination Sum

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi

【leetcode】Combination Sum II

Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including t

LeetCode 216. Combination Sum III (组合的和之三)

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n

【转载】Combination Sum

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi

LeetCode40 Combination Sum II

题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

LeetCode Combination Sum III

原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 与Combination Sum II相似,不同的是中不是所有元素相加,只是k个元素相加. 所以在把item的copy 加到res前需要同时满足item.size() == k 和 target == 0两个条件. AC Java: 1 public class Solution { 2 public List<List<Integer>> combinationS