Skip to content

Commit 841a715

Browse files
refactor 118
1 parent 59bc689 commit 841a715

File tree

1 file changed

+5
-13
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+5
-13
lines changed

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,13 @@ public class _118 {
2424
public static class Solution1 {
2525
public List<List<Integer>> generate(int numRows) {
2626
List<List<Integer>> result = new ArrayList();
27-
int len = 1;
27+
List<Integer> row = new ArrayList();
2828
for (int i = 0; i < numRows; i++) {
29-
List<Integer> row = new ArrayList(len);
30-
row.add(1);
31-
if (i > 0) {
32-
List<Integer> lastRow = result.get(i - 1);
33-
for (int j = 1; j < len; j++) {
34-
if (j < lastRow.size()) {
35-
row.add(lastRow.get(j - 1) + lastRow.get(j));
36-
}
37-
}
38-
row.add(1);
29+
row.add(0, 1);
30+
for (int j = 1; j < row.size() - 1; j++) {
31+
row.set(j, row.get(j) + row.get(j + 1));
3932
}
40-
result.add(row);
41-
len++;
33+
result.add(new ArrayList(row));
4234
}
4335
return result;
4436
}

0 commit comments

Comments
 (0)