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

Commit23e0ca9

Browse files
add a solution for 118
1 parentad8c02c commit23e0ca9

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
packagecom.fishercoder.solutions;
22

33
importjava.util.ArrayList;
4+
importjava.util.Arrays;
45
importjava.util.List;
56

67
publicclass_118 {
@@ -41,4 +42,28 @@ public List<List<Integer>> generate(int numRows) {
4142
returnresult;
4243
}
4344
}
45+
46+
publicstaticclassSolution3 {
47+
/**
48+
* my completely original solution on 9/15/2021
49+
*/
50+
publicList<List<Integer>>generate(intnumRows) {
51+
List<List<Integer>>ans =newArrayList<>();
52+
for (inti =0;i <numRows;i++) {
53+
if (ans.isEmpty()) {
54+
ans.add(Arrays.asList(1));
55+
}else {
56+
List<Integer>prev =ans.get(ans.size() -1);
57+
List<Integer>curr =newArrayList<>(prev.size() +1);
58+
curr.add(1);
59+
for (intj =0;j <prev.size() -1;j++) {
60+
curr.add(prev.get(j) +prev.get(j +1));
61+
}
62+
curr.add(1);
63+
ans.add(curr);
64+
}
65+
}
66+
returnans;
67+
}
68+
}
4469
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
publicclass_118Test {
99
privatestatic_118.Solution1solution1;
1010
privatestatic_118.Solution2solution2;
11+
privatestatic_118.Solution3solution3;
1112

1213
@BeforeClass
1314
publicstaticvoidsetup() {
1415
solution1 =new_118.Solution1();
1516
solution2 =new_118.Solution2();
17+
solution3 =new_118.Solution3();
1618
}
1719

1820
@Test
@@ -25,4 +27,9 @@ public void test2() {
2527
CommonUtils.printListList(solution2.generate(5));
2628
}
2729

30+
@Test
31+
publicvoidtest3() {
32+
CommonUtils.printListList(solution3.generate(5));
33+
}
34+
2835
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp