Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2f8fc22

Browse files
authored
Create Combination Sum IV.java
1 parent 5154e4d commit 2f8fc22

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Medium/Combination Sum IV.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int combinationSum4(int[] nums, int target) {
3+
Integer[] dp = new Integer[target + 1];
4+
return getCount(target, nums, dp);
5+
}
6+
7+
public int getCount(int target, int[] nums, Integer[] dp) {
8+
if (dp[target] != null) {
9+
return dp[target];
10+
}
11+
if (target == 0) {
12+
return 1;
13+
}
14+
if (target < 0) {
15+
return 0;
16+
}
17+
int total = 0;
18+
for (int num : nums) {
19+
if (target >= num) {
20+
total += getCount(target - num, nums, dp);
21+
}
22+
}
23+
return dp[target] = total;
24+
}
25+
}

0 commit comments

Comments
 (0)