Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd0ae983

Browse files
MEDIUM/src/medium/CombinationSumIII.java
1 parentd8448f0 commitd0ae983

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
packagemedium;
2+
3+
importjava.util.*;
4+
5+
publicclassCombinationSumIII {
6+
publicList<List<Integer>>combinationSum3(intk,intn) {
7+
List<List<Integer>>result =newArrayList();
8+
int[]nums =newint[] {1,2,3,4,5,6,7,8,9 };
9+
helper(k,n,nums,0,newArrayList(),result);
10+
returnresult;
11+
}
12+
13+
voidhelper(intk,intn,int[]nums,intstart,List<Integer>curr,List<List<Integer>>result) {
14+
if (n >0) {
15+
for (inti =start;i <nums.length;i++) {
16+
curr.add(nums[i]);
17+
helper(k,n -nums[i],nums,i +1,curr,result);// it needs to be a unique set of
18+
// numbers, so we need to set it
19+
// as i+1 here: each number is
20+
// used only once in this array:
21+
// [1,2,3,4,5,6,7,8,9]
22+
curr.remove(curr.size() -1);
23+
}
24+
}elseif (n ==0 &&curr.size() ==k) {//this is the major difference here: check size of curr list is of k before adding it
25+
List<Integer>temp =newArrayList(curr);
26+
result.add(temp);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp