File tree 1 file changed +18
-21
lines changed
1 file changed +18
-21
lines changed Original file line number Diff line number Diff line change 1
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 ];
2
+ public int combinationSum4 (int [] nums , int target ) {
3
+ Integer [] dp = new Integer [target + 1 ];
4
+ return helper (nums , target , dp );
10
5
}
11
- if (target == 0 ) {
12
- return 1 ;
6
+
7
+ private int helper (int [] nums , int target , Integer [] dp ) {
8
+ if (dp [target ] != null ) {
9
+ return dp [target ];
10
+ }
11
+ if (target == 0 ) {
12
+ return 1 ;
13
+ }
14
+ int count = 0 ;
15
+ for (int num : nums ) {
16
+ if (target >= num ) {
17
+ count += helper (nums , target - num , dp );
18
+ }
19
+ }
20
+ return dp [target ] = count ;
13
21
}
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
22
}
You can’t perform that action at this time.
0 commit comments