-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
48 lines (45 loc) · 1.27 KB
/
Solution.java
File metadata and controls
48 lines (45 loc) · 1.27 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
package Array.No_118_Pascal_Triangle;
import java.util.ArrayList;
import java.util.List;
/**
* FileName: Solution
* Author: EdisonLi的家用MacBook Pro
* Date: 2019-02-13 11:24
* Description: Pascal's Triangle
* <p>
* Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
* <p>
* <p>
* In Pascal's triangle, each number is the sum of the two numbers directly above it.
* <p>
* Example:
* <p>
* Input: 5
* Output:
* [
* [1],
* [1,1],
* [1,2,1],
* [1,3,3,1],
* [1,4,6,4,1]
* ]
*/
public class Solution {
public List<List<Integer>> generate(int numRows) {
ArrayList<List<Integer>> resList = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
ArrayList<Integer> temArray = new ArrayList<>();
for (int j = 0; j <= i; j++) {
if (j == 0) {
temArray.add(1);//头是一
} else if (j == i) {
temArray.add(1);//尾是一
} else {
temArray.add(resList.get(i - 1).get(j) + resList.get(i - 1).get(j - 1));//中间的数是上一层的该位置与上一层的该位置前一个数之和
}
}
resList.add(temArray);
}
return resList;
}
}