119. Pascal's Triangle II
2026/1/12小于 1 分钟约 246 字
119. Pascal's Triangle II
难度: Easy
题目描述
Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

Example 1:
Input: rowIndex = 3 Output: [1,3,3,1]
Example 2:
Input: rowIndex = 0 Output: [1]
Example 3:
Input: rowIndex = 1 Output: [1,1]
Constraints:
0 <= rowIndex <= 33
Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space?
解题思路
代码实现
解决方案
java
class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> pre = new ArrayList<>();
for (int i = 0; i <= rowIndex; i++) {
List<Integer> cur = new ArrayList<>();
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
cur.add(1);
} else {
cur.add(pre.get(j - 1) + pre.get(j));
}
}
pre = cur;
}
return pre;
}
}