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++) {
            // 由于数组是有序的
            // target - nums[i] < 0 后续的循环递归不可能找到解
            if(target - nums[i] < 0) {
                break;
            }
            listInt.add(nums[i]);
            // 递归调用的最后一个参数应该是i,而不是start
            // 如果是start则重复运算并且会出现重复解
            backTrack(list, listInt, nums, target - nums[i], i);
            // 回退一个位置继续求解
            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) 二分查找时间复杂度
Space:O(1) 不需要使用额外的存储空间,原地替换

Tips

回溯法

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • <center>#1 Two Sum</center> link Description:Given an arr...
    铛铛铛clark阅读 2,203评论 0 3
  • 如果你正在寻找一本关于寻找外星生命的手册,你很幸运。 这一领域的一些主要专家,包括加州大学河滨分校的一个研究小组,...
    wumingzhi111阅读 509评论 1 0
  • 红芋起出来之后,摔打摔打泥土,都扔到一堆。 推红芋干,最好选在耕过的土地上,推好就直接撒地里晒。 新耕过的土地,...
    DU杜默阅读 520评论 0 2
  • 火车上“艰难”地睡了一觉,终于等到天亮,枕头,被子全部贡献给空调排风口,宁愿冻着也不愿被风吹一晚,万一冻傻了怎么办...
    魅格体阅读 336评论 0 2
  • “少爷,不会变成植物人吧?” “林哥,您不用担心,少爷只是脑震荡,不用多久就会醒的……” “冷医生,您话是这么说的...
    白逸辰阅读 115评论 0 1