39. Combination Sum && 40. Combination Sum II && 216. Combination Sum III && 377. Combination Sum ...

39. 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.
  • 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]
]
Hide Tags
  Array Backtracking
 
class Solution {
  public List<List<Integer>> combinationSum(int[] candidates, int target) {
    Arrays.sort(candidates);

    ArrayList<List<Integer>> ret = new ArrayList<List<Integer>>();
    combinationSum(candidates, 0, target, new LinkedList<Integer>(), ret);
    return ret;
  }

  private void combinationSum(int[] candidates, int from, int target,
                              LinkedList<Integer> current, ArrayList<List<Integer>> ret)
  {
    for(int i = from; i<candidates.length; ++i)
    {
      int c = candidates[i];
      if(c > target)
        return;
      if(c == target)
      {
        LinkedList<Integer> sol = new LinkedList<Integer>(current);
        sol.add(c);
        ret.add(sol);
        return;
      }

      current.addLast(c);
      combinationSum(candidates, i, target -c, current, ret);
      current.removeLast();

    }
  }
}

 

40. 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 positive integers.
  • The solution set must not contain duplicate combinations.

For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8
A solution set is: 

[
  [1, 7],
  [1, 2, 5],
  [2, 6],
  [1, 1, 6]
]
Hide Tags
  Array Backtracking
Hide Similar Problems
  (M) Combination Sum
 
public class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
    Arrays.sort(candidates);
    
    ArrayList<List<Integer>> ret = new ArrayList<List<Integer>>();
    combinationSum(candidates, 0, target, new LinkedList<Integer>(), ret);
    return ret;
  }

  private void combinationSum(int[] candidates, int from, int target,
                              LinkedList<Integer> current, ArrayList<List<Integer>> ret)
  {
    Integer lastRemoved = null;
    
    for(int i = from; i<candidates.length; ++i)
    {
        int c = candidates[i];
        if(new Integer(c).equals(lastRemoved))
        //if(new Integer(c) == lastRemoved) //This is doing a reference equality check.
        //if(lastRemoved == c) //This give a java.lang.NullPointerException
            continue;
        
        if(c > target)
        return;
        if(c == target)
        {
        LinkedList<Integer> sol = new LinkedList<Integer>(current);
        sol.add(c);
        ret.add(sol);
        return;
        }
        
        current.addLast(c);
        combinationSum(candidates, i+1, target -c, current, ret);
        lastRemoved = current.removeLast();

    }
  }
}

 

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 = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]
Hide Tags
  Array Backtracking
Hide Similar Problems
  (M) Combination Sum

 

public class Solution {
    
    public List<List<Integer>> combinationSum3(int k, int n) {
    
    int[] candidates = new int[]{1,2,3,4,5,6,7,8,9};
    int target = n;
    int count = k;
    
    ArrayList<List<Integer>> ret = new ArrayList<List<Integer>>();
    combinationSum(count, candidates, 0, target, new LinkedList<Integer>(), ret);
    return ret;
  }

  private void combinationSum(int count, int[] candidates, int from, int target,
                              LinkedList<Integer> current, ArrayList<List<Integer>> ret)
  {
        
    Integer lastRemoved = null;
    for(int i = from; i<candidates.length; ++i)
    {
        Integer c = candidates[i];
        if(c.equals(lastRemoved))
            continue;
        if(c > target)
            return;
        if(c.equals(target))
        {
            if(count>1)
                return;
            LinkedList<Integer> sol = new LinkedList<Integer>(current);
            sol.add(c);
            ret.add(sol);
            return;
        }
    
        if(count<=1)
            continue;
        current.addLast(c);
        combinationSum(--count, candidates, i+1, target -c, current, ret);
        ++count;
        lastRemoved = current.removeLast();
    
    }
  }
  
}

 

Solution2:

public class Solution {
    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> ans = new ArrayList<>();
        combination(ans, new ArrayList<Integer>(), k, 1, n);
        return ans;
    }

    private void combination(List<List<Integer>> ans, List<Integer> comb, int k,  int start, int n) {
        if (comb.size() == k) {
            if(n == 0) {
                List<Integer> li = new ArrayList<Integer>(comb);
                ans.add(li);
            }
            return;
        }
        
        for (int i = start; i <= 9; i++) {
            if(i>n)
                return;
            comb.add(i);
            combination(ans, comb, k, i+1, n-i);
            comb.remove(comb.size() - 1);
        }
    }
}

 

 

377. Combination Sum IV

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

Example:

nums = [1, 2, 3]
target = 4

The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Note that different sequences are counted as different combinations.
Therefore the output is 7.

Follow up:
What if negative numbers are allowed in the given array?
How does it change the problem?
What limitation we need to add to the question to allow negative numbers?

 
Hide Tags
  Dynamic Programming
Hide Similar Problems
  (M) Combination Sum
 
 
public class Solution {
  public int combinationSum4(int[] nums, int target) {
    List<Integer> newNums = new ArrayList<>();
    for (Integer n : nums)
      if (n <= target)
        newNums.add(n);
    Collections.sort(newNums);

    int[] results = new int[target + 1];
    for (int sum = 1; sum < results.length; ++sum) {
      for (int n : newNums) {
        if (n > sum)
          break;
        else if (n == sum)
          results[sum] += 1;
        else
          results[sum] += results[sum - n];
      }
    }
    return results[target];
  }
}

 

 

 

 

转载于:https://www.cnblogs.com/neweracoding/p/5574660.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值