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

Commitaa68be3

Browse files
MEDIUM/src/medium/Combinations.java
1 parent0da9fe6 commitaa68be3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎MEDIUM/src/medium/Combinations.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
packagemedium;
2+
importjava.util.*;
3+
4+
publicclassCombinations {
5+
//I'm glad that I made this one AC'ed the first time I submitted it.
6+
//After finishing three similar problems! Cool!
7+
8+
publicList<List<Integer>>combine(intn,intk) {
9+
List<List<Integer>>result =newArrayList();
10+
int[]nums =newint[n];
11+
for(inti =0;i <n;i++){
12+
nums[i] =i+1;
13+
}
14+
backtracking(k,0,nums,newArrayList(),result);
15+
returnresult;
16+
}
17+
18+
voidbacktracking(intk,intstart,int[]nums,List<Integer>temp,List<List<Integer>>result){
19+
if(temp.size() ==k){
20+
List<Integer>newTemp =newArrayList(temp);
21+
result.add(newTemp);
22+
}elseif(temp.size() <k){
23+
for(inti =start;i <nums.length;i++){
24+
temp.add(nums[i]);
25+
backtracking(k,i+1,nums,temp,result);
26+
temp.remove(temp.size()-1);
27+
}
28+
}
29+
}
30+
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp