-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPascalsTriangle.java
More file actions
49 lines (46 loc) · 1.17 KB
/
PascalsTriangle.java
File metadata and controls
49 lines (46 loc) · 1.17 KB
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
42
43
44
45
46
47
48
49
package leetcode;
import java.util.ArrayList;
import java.util.List;
/**
* @author eko
* @date 2018/10/29 10:47 AM
*
* Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
*
*
* In Pascal's triangle, each number is the sum of the two numbers directly above it.
*
* Example:
*
* Input: 5
* Output:
* [
* [1],
* [1,1],
* [1,2,1],
* [1,3,3,1],
* [1,4,6,4,1]
* ]
*/
public class PascalsTriangle {
public static void main(String[] args) {
List<List<Integer>> res = new PascalsTriangle().generate(5);
System.out.println(res);
}
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
List<Integer> row = new ArrayList<>();
List<Integer> lastRow = new ArrayList<>();
if (i != 0) {
lastRow = res.get(i - 1);
}
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) row.add(1);
else row.add(lastRow.get(j - 1) + lastRow.get(j));
}
res.add(row);
}
return res;
}
}