-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0118_PascalsTriangle.java
More file actions
65 lines (60 loc) · 2.11 KB
/
P0118_PascalsTriangle.java
File metadata and controls
65 lines (60 loc) · 2.11 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package yyl.leetcode.p01;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import yyl.leetcode.util.Assert;
/**
* <h3>杨辉三角</h3><br>
* 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。<br>
* 在杨辉三角中,每个数是它左上方和右上方的数的和。<br>
*
* <pre>
* 示例:
* 输入: 5
* 输出:
* [
* [1],
* [1,1],
* [1,2,1],
* [1,3,3,1],
* [1,4,6,4,1]
* ]
* </pre>
*/
public class P0118_PascalsTriangle {
public static void main(String[] args) {
Solution solution = new Solution();
List<List<Integer>> expected = new ArrayList<>();
expected.add(Arrays.asList(1));
expected.add(Arrays.asList(1, 1));
expected.add(Arrays.asList(1, 2, 1));
expected.add(Arrays.asList(1, 3, 3, 1));
expected.add(Arrays.asList(1, 4, 6, 4, 1));
List<List<Integer>> actual = solution.generate(5);
Assert.assertEquals(expected, actual);
}
// 迭代(根据定义直接求解)
// 从第二行开始,将当前行 List<Integer>对象中的首位元素跟末位元素添加整数1,首位跟末位之间的元素为与当前行元素索引相同的上一行元素值,跟此时上一行相同索引元素的前一个的和。
// 时间复杂度:O(numRows^2)。
// 空间复杂度:O(1)。不考虑返回值的空间占用。
static class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> answer = new ArrayList<>();
if (numRows == 0) {
return answer;
}
answer.add(Arrays.asList(1));
for (int i = 1; i < numRows; i++) {
List<Integer> previousRow = answer.get(i - 1);
List<Integer> currentRow = new ArrayList<>(i);
currentRow.add(1);
for (int j = 1; j < i; j++) {
currentRow.add(previousRow.get(j - 1) + previousRow.get(j));
}
currentRow.add(1);
answer.add(currentRow);
}
return answer;
}
}
}