Combination Sum | & ||

Posted 北叶青藤

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Combination Sum | & ||相关的知识,希望对你有一定的参考价值。

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.

For example, given candidate set 2,3,6,7 and target 7
A solution set is: 
[7] 
[2, 2, 3] 

 Notice
  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.
Example

given candidate set 2,3,6,7 and target 7
A solution set is: 
[7] 
[2, 2, 3] 

分析:递归

 1 public class Solution {
 2     public List<List<Integer>> combinationSum(int[] candidates, int target) {
 3         List<List<Integer>> listsAll = new ArrayList<>();
 4         Arrays.sort(candidates);
 5         helper(0, 0, candidates, target, new ArrayList<>(), listsAll);
 6         return listsAll;
 7     }
 8     
 9     public static void helper(int index, int total, int[] candidates, int target, List<Integer> list, List<List<Integer>> listsAll) {
10         if (index >= candidates.length || total > target) return;
11         if (total == target) {
12             listsAll.add(new ArrayList<>(list));
13             return;
14         }
15         list.add(candidates[index]);
16         helper(index, total + candidates[index], candidates, target, list, listsAll);
17         list.remove(list.size() - 1);
18         helper(index + 1, total, candidates, target, list, listsAll);        
19     }
20 }

 

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.

 Notice
  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

Given candidate set [10,1,6,7,2,1,5] and target 8,

A solution set is:

[
  [1,7],
  [1,2,5],
  [2,6],
  [1,1,6]
]
 1 public class Solution {
 2     public List<List<Integer>> combinationSum2(int[] candidates, int target) {
 3         List<List<Integer>> listsAll = new ArrayList<List<Integer>>();
 4         Arrays.sort(candidates);
 5         helper(0, 0, candidates, target, new ArrayList<>(), listsAll);
 6         return listsAll;
 7     }
 8     
 9     public static void helper(int index, int total, int[] candidates, int target, List<Integer> list, List<List<Integer>> listsAll) {
10         if (total == target) {
11             listsAll.add(new ArrayList<Integer>(list));
12             return;
13         }
14         if (index >= candidates.length || total > target) return;
15         list.add(candidates[index]);
16 
17         helper(index + 1, total + candidates[index], candidates, target, list, listsAll);
18         list.remove(list.size() - 1);
19         while (index + 1 < candidates.length && candidates[index] == candidates[index + 1]) {
20             index++;
21         }
22         helper(index + 1, total, candidates, target, list, listsAll);        
23     }
24 }

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.
分析: 这题和change coin非常相似。
 1 public class Solution {
 2     public int combinationSum4(int[] nums, int target) {
 3         if (nums == null || nums.length == 0) return 0;
 4 
 5         int[] dp = new int[target + 1];
 6         dp[0] = 1;
 7 
 8         for (int i = 1; i <= target; i++) {
 9             for (int num : nums) {
10                 if (i - num >= 0) {
11                     dp[i] += dp[i - num];
12                 }
13             }
14         }
15         return dp[target];
16     }
17 }

参考请注明出处:cnblogs.com/beiyeqingteng/ 

以上是关于Combination Sum | & ||的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode题意分析&解答39. Combination Sum

Combination Sum

Combination Sum II

377. Combination Sum IV (DP)

LeetCode-40. Combination Sum II

40. Combination Sum II