LeetCode036-Combination Sum

Combination Sum

Question:

Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

The same repeated number may be chosen from candidates unlimited number of times.

Note:

All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]

Example 2:

Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]

解法代碼

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LeetCode39 {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(candidates);
        backTrack(list, new ArrayList<Integer>(), candidates, target, 0);
        
        return list;
    }
    
    public void backTrack(List<List<Integer>> list, 
            List<Integer> listInt, int[] nums, int target, int start) {
        if(target == 0) {
            // 添加找到的解,一定要深拷貝一個對象保存到list中
            // 因為求解過程中始終用的listInt對象
            list.add(new ArrayList<Integer>(listInt));
            return;
        }
        for(int i = start; i < nums.length; i++) {
            // 由于數(shù)組是有序的
            // target - nums[i] < 0 后續(xù)的循環(huán)遞歸不可能找到解
            if(target - nums[i] < 0) {
                break;
            }
            listInt.add(nums[i]);
            // 遞歸調(diào)用的最后一個參數(shù)應(yīng)該是i,而不是start
            // 如果是start則重復(fù)運算并且會出現(xiàn)重復(fù)解
            backTrack(list, listInt, nums, target - nums[i], i);
            // 回退一個位置繼續(xù)求解
            listInt.remove(listInt.size() - 1);
        }
    }
}

測試代碼

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import com.kay.pro.alog.LeetCode39;

@RunWith(Parameterized.class)
public class LeetCode39Test {
    private LeetCode39 leetCode;
    private int[] nums;
    private int target;
    private List<List<Integer>> ret;
    
    public LeetCode39Test(int[] nums, int target, List<List<Integer>> ret) {
        this.nums = nums;
        this.target = target;
        this.ret = ret;
    }
    
    @Before
    public void before() {
        leetCode = new LeetCode39();
    }
    
    @Parameters
    public static Collection<?> reverserArrays() {
        List<List<Integer>> list1 = new ArrayList<List<Integer>>();
        list1.add(Arrays.asList(2, 2, 3));
        list1.add(Arrays.asList(7));
        
        List<List<Integer>> list2 = new ArrayList<List<Integer>>();
        list2.add(Arrays.asList(2, 2, 2, 2));
        list2.add(Arrays.asList(2, 3, 3));
        list2.add(Arrays.asList(3, 5));
        
        return Arrays.asList(new Object[][]{
            {new int[]{2,3,6,7}, 7, list1},
            {new int[]{2,3,5}, 8, list2}
        });
    }
    
    @Test
    public void leetCode33() {
        assertEquals(ret, leetCode.combinationSum(nums, target));
    }
}

Output:

Time And Space Complexity

Time: O(logn) 二分查找時間復(fù)雜度
Space:O(1) 不需要使用額外的存儲空間,原地替換

Tips

回溯法

?著作權(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)容

  • <center>#1 Two Sum</center> link Description:Given an arr...
    鐺鐺鐺clark閱讀 2,374評論 0 3
  • 如果你正在尋找一本關(guān)于尋找外星生命的手冊,你很幸運。 這一領(lǐng)域的一些主要專家,包括加州大學(xué)河濱分校的一個研究小組,...
    wumingzhi111閱讀 602評論 1 0
  • 紅芋起出來之后,摔打摔打泥土,都扔到一堆。 推紅芋干,最好選在耕過的土地上,推好就直接撒地里曬。 新耕過的土地,...
    DU杜默閱讀 569評論 0 2
  • 火車上“艱難”地睡了一覺,終于等到天亮,枕頭,被子全部貢獻給空調(diào)排風(fēng)口,寧愿凍著也不愿被風(fēng)吹一晚,萬一凍傻了怎么辦...
    魅格體閱讀 394評論 0 2
  • “少爺,不會變成植物人吧?” “林哥,您不用擔(dān)心,少爺只是腦震蕩,不用多久就會醒的……” “冷醫(yī)生,您話是這么說的...
    白逸辰閱讀 128評論 0 1

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