39. Combination Sum

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 C unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[
[7],
[2, 2, 3]
]

Solution1:Backtracking(DFS)

總結(jié)見:http://www.itdecent.cn/p/883fdda93a66
思路:回溯法,類似深度優(yōu)先進行組合,cur_result數(shù)組保持用當(dāng)前嘗試的結(jié)果,并按照深度優(yōu)先次序依次將組合結(jié)果加入到result_list中,因為可以重復(fù)選擇,start_next 仍= i, DFS到底后 step back (通過remove 當(dāng)前cur_result的最后一位),換下一個嘗試組合,后繼續(xù)DFS重復(fù)此過程,實現(xiàn)上采用遞歸方式。
排序后可以部分剪枝,提前返回結(jié)果 加速。
回溯順序:
輸入[1, 2, 3]
[1, 1, 1] [1, 1, 2] [1, 1, 3] [1, 2, 2] [1, 2, 3] [1, 3, 3]
[2, 2, 2] [2, 2, 3] ...

Time Complexity: O((target_test_time) ^ N) since there could be a 重復(fù)情況
Space Complexity(不算result的話): O(2n) : n是遞歸緩存的cur_result + n是緩存了n層的普通變量O(1) ? (Not Sure)

Solution1 Code:

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> cur_res = new ArrayList<>();
        Arrays.sort(candidates); //for possible early stop
        backtrack(candidates, 0, target, cur_res, result);
        return result;
    }

    private void backtrack(int[] nums, int start, int remain, List<Integer> cur_res, List<List<Integer>> result) {
        if(remain < 0) // early stop
            return;
        else if(remain == 0)
            result.add(new ArrayList<>(cur_res));
        else {
            for(int i = start; i < nums.length; i++) {
                cur_res.add(nums[i]);
                backtrack(nums, i, remain - nums[i], cur_res, result);
                cur_res.remove(cur_res.size() - 1);
            }
        }
    }
}

Solution1.Round1 Code:

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        
        List<List<Integer>> result = new ArrayList<>();
        if(candidates == null || candidates.length == 0) {
            return result;
        }
        List<Integer> cur_res = new ArrayList<>();
        
        // Arrays.sort(candidates);
        backtrack(candidates, 0, 0, cur_res, result, target);
        return result;
        
    }
    
            
    private void backtrack(int[] candidates, int start, int cur_sum, List<Integer> cur_res, List<List<Integer>> result, int target) {
        if(cur_sum == target) {
            result.add(new ArrayList<>(cur_res));
            return;
        }
        else if(cur_sum > target) {
            return;
        }

        for(int i = start; i < candidates.length; i++) {
            cur_res.add(candidates[i]);
            backtrack(candidates, i, cur_sum + candidates[i], cur_res, result, target);
            cur_res.remove(cur_res.size() - 1);
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容