Skip to content

Commit 63a0878

Browse files
committed
feat: add 118
1 parent a4faad7 commit 63a0878

File tree

6 files changed

+193
-105
lines changed

6 files changed

+193
-105
lines changed

note/118/README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# [Pascal's Triangle][title]
2+
3+
## Description
4+
5+
Given *numRows*, generate the first *numRows* of Pascal's triangle.
6+
7+
For example, given *numRows* = 5,
8+
Return
9+
10+
```
11+
[
12+
[1],
13+
[1,1],
14+
[1,2,1],
15+
[1,3,3,1],
16+
[1,4,6,4,1]
17+
]
18+
```
19+
20+
**Tags:** Array
21+
22+
23+
## 思路
24+
25+
题意是给出行数,输出帕斯卡尔三角形,很简单的模拟,就不多说了。
26+
27+
``` java
28+
class Solution {
29+
public List<List<Integer>> generate(int numRows) {
30+
if (numRows == 0) return Collections.emptyList();
31+
List<List<Integer>> list = new ArrayList<>();
32+
for (int i = 0; i < numRows; ++i) {
33+
List<Integer> sub = new ArrayList<>();
34+
for (int j = 0; j <= i; ++j) {
35+
if (j == 0 || j == i) {
36+
sub.add(1);
37+
} else {
38+
List<Integer> upSub = list.get(i - 1);
39+
sub.add(upSub.get(j - 1) + upSub.get(j));
40+
}
41+
}
42+
list.add(sub);
43+
}
44+
return list;
45+
}
46+
}
47+
```
48+
49+
50+
## 结语
51+
52+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
53+
54+
55+
56+
[title]: https://leetcode.com/problems/pascals-triangle
57+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

0 commit comments

Comments
 (0)