File tree 2 files changed +32
-0
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder
2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
3
import java .util .ArrayList ;
4
+ import java .util .Arrays ;
4
5
import java .util .List ;
5
6
6
7
public class _118 {
@@ -41,4 +42,28 @@ public List<List<Integer>> generate(int numRows) {
41
42
return result ;
42
43
}
43
44
}
45
+
46
+ public static class Solution3 {
47
+ /**
48
+ * my completely original solution on 9/15/2021
49
+ */
50
+ public List <List <Integer >> generate (int numRows ) {
51
+ List <List <Integer >> ans = new ArrayList <>();
52
+ for (int i = 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 = new ArrayList <>(prev .size () + 1 );
58
+ curr .add (1 );
59
+ for (int j = 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
+ return ans ;
67
+ }
68
+ }
44
69
}
Original file line number Diff line number Diff line change 8
8
public class _118Test {
9
9
private static _118 .Solution1 solution1 ;
10
10
private static _118 .Solution2 solution2 ;
11
+ private static _118 .Solution3 solution3 ;
11
12
12
13
@ BeforeClass
13
14
public static void setup () {
14
15
solution1 = new _118 .Solution1 ();
15
16
solution2 = new _118 .Solution2 ();
17
+ solution3 = new _118 .Solution3 ();
16
18
}
17
19
18
20
@ Test
@@ -25,4 +27,9 @@ public void test2() {
25
27
CommonUtils .printListList (solution2 .generate (5 ));
26
28
}
27
29
30
+ @ Test
31
+ public void test3 () {
32
+ CommonUtils .printListList (solution3 .generate (5 ));
33
+ }
34
+
28
35
}
You can’t perform that action at this time.
0 commit comments