LeetCode - Combination Sum I && II

Combination Sum I

https://leetcode.com/problems/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 (including target) will be positive integers.
  • Elements in a combination (a1a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • 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] 

这种要求返回所有的题都可以用递归。

这里要注意的有3点:

1. 要求不能有重复,由于每层递归对应每个位置,因此当前位置(当前层的递归)上,不能用同一个数,因此遇到相同的数要略过,因此首先要排序,然后见for循环中的while循环。

2. 重复情况还有一种是[1,3], [3,1]这种,虽然每个位置数不一样,但本质其实是一样的,要避免这种情况,是在helper函数中加start变量,保证后面位置只用当前位置用的数的后面的数。

3. 解中的数要求是递增的。因此对数组排序。

代码如下:

public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> rst = new LinkedList<List<Integer>>();
        if(candidates==null || candidates.length==0) return rst;
        Arrays.sort(candidates);   //排序,保证递增以及避免重复
        List<Integer> solution = new LinkedList<Integer>();
        
        helper(candidates, rst, solution, target, 0);
        
        return rst;
    }
    
    public void helper(int[] candidates, List<List<Integer>> rst, List<Integer> solution, int target, int start){
        if(target==0){
            rst.add(new LinkedList<Integer>(solution));
            return;
        }
        if(target<0) return;
        for(int i=start; i<candidates.length; i++){   //i从start开始,当前位置只能使用之前没有看过的数
            solution.add(candidates[i]);
            helper(candidates, rst, solution, target-candidates[i], i);
            solution.remove(solution.size()-1);
            while(i<candidates.length-1 && candidates[i+1]==candidates[i]) i++;  //为当前位置略过重复数
        }
    }
}

时间复杂度和空间复杂度都是exponential的。


Combination Sum II

https://leetcode.com/problems/combination-sum-ii/

这道题跟上一道题基本一模一样,唯一区别就是每个数只能用一次,因此代码上只需要改一点点就行,即下一层递归不能再从当前数开始,而要从下一个数开始了。

代码如下:

public class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> rst = new LinkedList<List<Integer>>();
        if(candidates==null || candidates.length==0) return rst;
        Arrays.sort(candidates);
        List<Integer> solution = new LinkedList<Integer>();
        
        helper(candidates, rst, solution, target, 0);
        
        return rst;
    }

    public void helper(int[] candidates, List<List<Integer>> rst, List<Integer> solution, int target, int start){
        if(target==0){
            rst.add(new LinkedList<Integer>(solution));
            return;
        }
        if(target<0) return;
        for(int i=start; i<candidates.length; i++){
            solution.add(candidates[i]);
            helper(candidates, rst, solution, target-candidates[i], i+1);  //跟上一题唯一不同的就是,上一题最后一个参数是i,这里是i+1
            solution.remove(solution.size()-1);
            while(i<candidates.length-1 && candidates[i+1]==candidates[i]) i++;
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值