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

Commitad74745

Browse files
add a solution for 77
1 parent59b6945 commitad74745

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_77.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,31 @@ private void backtracking(List<Integer> list, int k, int start, int limit, List<
3131
}
3232
}
3333
}
34+
35+
publicstaticclassSolution2 {
36+
/**
37+
* My completely own solution on 1/24/2022.
38+
*/
39+
publicList<List<Integer>>combine(intn,intk) {
40+
List<List<Integer>>ans =newArrayList<>();
41+
int[]nums =newint[n];
42+
for (inti =1;i <=n;i++) {
43+
nums[i -1] =i;
44+
}
45+
backtrack(ans,nums,k,newArrayList<>(),0);
46+
returnans;
47+
}
48+
49+
privatevoidbacktrack(List<List<Integer>>ans,int[]nums,intk,List<Integer>curr,intstart) {
50+
if (curr.size() ==k) {
51+
ans.add(newArrayList<>(curr));
52+
}elseif (curr.size() <k) {
53+
for (inti =start;i <nums.length;i++) {
54+
curr.add(nums[i]);
55+
backtrack(ans,nums,k,curr,i +1);
56+
curr.remove(curr.size() -1);
57+
}
58+
}
59+
}
60+
}
3461
}

‎src/test/java/com/fishercoder/_77Test.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77

88
publicclass_77Test {
99
privatestatic_77.Solution1solution1;
10+
privatestatic_77.Solution2solution2;
1011

1112
@BeforeClass
1213
publicstaticvoidsetup() {
1314
solution1 =new_77.Solution1();
15+
solution2 =new_77.Solution2();
1416
}
1517

1618
@Test
1719
publicvoidtest1() {
1820
CommonUtils.printListList(solution1.combine(4,2));
21+
CommonUtils.printListList(solution2.combine(4,2));
1922
}
2023
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp