|
5 | 5 | import java.util.ArrayList;
|
6 | 6 | import java.util.Arrays;
|
7 | 7 | import java.util.List;
|
8 |
| -/**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. |
| 8 | +/**Given an integer array with all positive numbers and no duplicates, |
| 9 | + * find the number of possible combinations that add up to a positive integer target. |
9 | 10 |
|
10 | 11 | Example:
|
11 | 12 |
|
|
24 | 25 | Note that different sequences are counted as different combinations.
|
25 | 26 |
|
26 | 27 | Therefore the output is 7.
|
| 28 | +
|
27 | 29 | Follow up:
|
28 | 30 | What if negative numbers are allowed in the given array?
|
29 | 31 | How does it change the problem?
|
30 | 32 | What limitation we need to add to the question to allow negative numbers?*/
|
31 |
| -public class CombinationSumIV { |
32 |
| - //since this question doesn't require to return all the combination result, instead, it just wants one number, we could use DP |
33 |
| - //the idea is similar to Climbing Stairs. |
34 |
| - //adopted this solution: https://discuss.leetcode.com/topic/52186/my-3ms-java-dp-solution |
| 33 | +public class _377 { |
| 34 | + /**since this question doesn't require to return all the combination result, instead, it just wants one number, we could use DP |
| 35 | + the idea is similar to Climbing Stairs. |
| 36 | + adopted this solution: https://discuss.leetcode.com/topic/52186/my-3ms-java-dp-solution*/ |
35 | 37 | public int combinationSum4(int[] nums, int target){
|
36 | 38 | Arrays.sort(nums);
|
37 | 39 | int[] result = new int[target+1];
|
@@ -68,7 +70,7 @@ private void backtracking(int start, int[] nums, int target, ArrayList temp,
|
68 | 70 | }
|
69 | 71 |
|
70 | 72 | public static void main(String...strings){
|
71 |
| - CombinationSumIV test = new CombinationSumIV(); |
| 73 | + _377 test = new _377(); |
72 | 74 | int[] nums = new int[]{1,2,3};
|
73 | 75 | int target = 4;
|
74 | 76 | CommonUtils.printListList(test.combinationSum4_printout(nums, target));
|
|
0 commit comments