File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 22
22
public class _118 {
23
23
24
24
public static class Solution1 {
25
+ /**fill out values from left to right*/
25
26
public List <List <Integer >> generate (int numRows ) {
26
27
List <List <Integer >> result = new ArrayList ();
27
28
List <Integer > row = new ArrayList ();
@@ -35,4 +36,21 @@ public List<List<Integer>> generate(int numRows) {
35
36
return result ;
36
37
}
37
38
}
39
+
40
+ public static class Solution2 {
41
+ /**fill out values from right to left
42
+ * credit: https://leetcode.com/problems/pascals-triangle/discuss/38141/My-concise-solution-in-Java/36127*/
43
+ public List <List <Integer >> generate (int numRows ) {
44
+ List <List <Integer >> result = new ArrayList ();
45
+ List <Integer > row = new ArrayList ();
46
+ for (int i = 0 ; i < numRows ; i ++) {
47
+ for (int j = row .size () - 1 ; j >= 1 ; j --) {
48
+ row .set (j , row .get (j ) + row .get (j - 1 ));
49
+ }
50
+ row .add (1 );
51
+ result .add (new ArrayList (row ));
52
+ }
53
+ return result ;
54
+ }
55
+ }
38
56
}
Original file line number Diff line number Diff line change
1
+ package com .fishercoder ;
2
+
3
+ import com .fishercoder .common .utils .CommonUtils ;
4
+ import com .fishercoder .solutions ._118 ;
5
+ import org .junit .BeforeClass ;
6
+ import org .junit .Test ;
7
+
8
+ public class _118Test {
9
+ private static _118 .Solution1 solution1 ;
10
+ private static _118 .Solution2 solution2 ;
11
+
12
+ @ BeforeClass
13
+ public static void setup () {
14
+ solution1 = new _118 .Solution1 ();
15
+ solution2 = new _118 .Solution2 ();
16
+ }
17
+
18
+ @ Test
19
+ public void test1 () {
20
+ CommonUtils .printListList (solution1 .generate (5 ));
21
+ CommonUtils .printListList (solution2 .generate (5 ));
22
+ }
23
+
24
+ }
You can’t perform that action at this time.
0 commit comments