Skip to content

Commit ce29350

Browse files
committed
feat: add 119
1 parent 63a0878 commit ce29350

File tree

6 files changed

+162
-164
lines changed

6 files changed

+162
-164
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
|110|[Balanced Binary Tree][110]|Tree, Depth-first Search|
3737
|111|[Minimum Depth of Binary Tree][111]|Tree, Depth-first Search, Breadth-first Search|
3838
|112|[Path Sum][112]|Tree, Depth-first Search|
39+
|118|[Pascal's Triangle][118]|Array|
40+
|119|[Pascal's Triangle II][119]|Array|
3941

4042

4143
## Medium
@@ -87,6 +89,8 @@
8789
[110]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/110/README.md
8890
[111]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/111/README.md
8991
[112]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/112/README.md
92+
[118]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/118/README.md
93+
[119]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/119/README.md
9094

9195
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
9296
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md

note/119/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# [Pascal's Triangle II][title]
2+
3+
## Description
4+
5+
Given an index *k*, return the *k*th row of the Pascal's triangle.
6+
7+
For example, given *k* = 3,
8+
Return `[1,3,3,1]`.
9+
10+
**Note:**
11+
Could you optimize your algorithm to use only *O*(*k*) extra space?
12+
13+
**Tags:** Array
14+
15+
16+
## 思路
17+
18+
题意是指定输出帕斯卡尔三角形的某一行,模拟即可,优化后的代码如下所示。
19+
20+
``` java
21+
class Solution {
22+
public List<Integer> getRow(int rowIndex) {
23+
List<Integer> res = new ArrayList<>();
24+
for (int i = 0; i <= rowIndex; ++i) {
25+
res.add(1);
26+
for (int j = i - 1; j > 0; --j) {
27+
res.set(j, res.get(j - 1) + res.get(j));
28+
}
29+
}
30+
return res;
31+
}
32+
}
33+
```
34+
35+
36+
## 结语
37+
38+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
39+
40+
41+
42+
[title]: https://leetcode.com/problems/pascals-triangle-ii
43+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

0 commit comments

Comments
 (0)