Skip to content

Commit c8daecc

Browse files
add one more solution for 118 and test
1 parent 841a715 commit c8daecc

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
public class _118 {
2323

2424
public static class Solution1 {
25+
/**fill out values from left to right*/
2526
public List<List<Integer>> generate(int numRows) {
2627
List<List<Integer>> result = new ArrayList();
2728
List<Integer> row = new ArrayList();
@@ -35,4 +36,21 @@ public List<List<Integer>> generate(int numRows) {
3536
return result;
3637
}
3738
}
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+
}
3856
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)