forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_118.java
41 lines (36 loc) · 1009 Bytes
/
_118.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.fishercoder.solutions;
import java.util.*;
/**Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]*/
public class _118 {
public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList();
if(numRows < 1) return result;
List<Integer> row = new ArrayList();
row.add(1);
result.add(row);
for(int i = 1; i < numRows; i++){
List<Integer> newRow = new ArrayList();
newRow.add(1);
List<Integer> lastRow = result.get(i-1);
for(int j = 1; j < lastRow.size(); j++){
newRow.add(lastRow.get(j-1) + lastRow.get(j));
}
newRow.add(1);
result.add(newRow);
}
return result;
}
public static void main(String...strings){
int numRows = 2;
generate(numRows);
}
}