Skip to content

Commit 483e1a5

Browse files
refactor 118
1 parent 3fa4585 commit 483e1a5

File tree

1 file changed

+31
-43
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+31
-43
lines changed

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

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,42 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
/**
7-
* 118. Pascal's Triangle
8-
* Given numRows, generate the first numRows of Pascal's triangle.
9-
10-
For example, given numRows = 5,
11-
Return
12-
13-
[
14-
[1],
15-
[1,1],
16-
[1,2,1],
17-
[1,3,3,1],
18-
[1,4,6,4,1]
19-
]
20-
21-
*/
226
public class _118 {
237

24-
public static class Solution1 {
25-
/**fill out values from left to right*/
26-
public List<List<Integer>> generate(int numRows) {
27-
List<List<Integer>> result = new ArrayList();
28-
List<Integer> row = new ArrayList();
29-
for (int i = 0; i < numRows; i++) {
30-
row.add(0, 1);
31-
for (int j = 1; j < row.size() - 1; j++) {
32-
row.set(j, row.get(j) + row.get(j + 1));
8+
public static class Solution1 {
9+
/**
10+
* fill out values from left to right
11+
*/
12+
public List<List<Integer>> generate(int numRows) {
13+
List<List<Integer>> result = new ArrayList();
14+
List<Integer> row = new ArrayList();
15+
for (int i = 0; i < numRows; i++) {
16+
row.add(0, 1);
17+
for (int j = 1; j < row.size() - 1; j++) {
18+
row.set(j, row.get(j) + row.get(j + 1));
19+
}
20+
result.add(new ArrayList(row));
21+
}
22+
return result;
3323
}
34-
result.add(new ArrayList(row));
35-
}
36-
return result;
3724
}
38-
}
3925

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));
26+
public static class Solution2 {
27+
/**
28+
* fill out values from right to left
29+
* credit: https://leetcode.com/problems/pascals-triangle/discuss/38141/My-concise-solution-in-Java/36127
30+
*/
31+
public List<List<Integer>> generate(int numRows) {
32+
List<List<Integer>> result = new ArrayList();
33+
List<Integer> row = new ArrayList();
34+
for (int i = 0; i < numRows; i++) {
35+
for (int j = row.size() - 1; j >= 1; j--) {
36+
row.set(j, row.get(j) + row.get(j - 1));
37+
}
38+
row.add(1);
39+
result.add(new ArrayList<>(row));
40+
}
41+
return result;
4942
}
50-
row.add(1);
51-
result.add(new ArrayList<>(row));
52-
}
53-
return result;
5443
}
55-
}
5644
}

0 commit comments

Comments
 (0)