# [119. Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii) [中文文档](/solution/0100-0199/0119.Pascal%27s%20Triangle%20II/README.md) ## Description <p>Given an integer <code>rowIndex</code>, return the <code>rowIndex<sup>th</sup></code> (<strong>0-indexed</strong>) row of the <strong>Pascal's triangle</strong>.</p> <p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p> <img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0119.Pascal%27s%20Triangle%20II/images/PascalTriangleAnimated2.gif" style="height:240px; width:260px" /> <p> </p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> rowIndex = 3 <strong>Output:</strong> [1,3,3,1] </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> rowIndex = 0 <strong>Output:</strong> [1] </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> rowIndex = 1 <strong>Output:</strong> [1,1] </pre> <p> </p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 <= rowIndex <= 33</code></li> </ul> <p> </p> <p><strong>Follow up:</strong> Could you optimize your algorithm to use only <code>O(rowIndex)</code> extra space?</p> ## Solutions <!-- tabs:start --> ### **Python3** ```python class Solution: def getRow(self, rowIndex: int) -> List[int]: row = [1] * (rowIndex + 1) for i in range(2, rowIndex + 1): for j in range(i - 1, 0, -1): row[j] += row[j - 1] return row ``` ### **Java** ```java class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> row = new ArrayList<>(); for (int i = 0; i < rowIndex + 1; ++i) { row.add(1); } for (int i = 2; i < rowIndex + 1; ++i) { for (int j = i - 1; j > 0; --j) { row.set(j, row.get(j) + row.get(j - 1)); } } return row; } } ``` ### **TypeScript** ```ts function getRow(rowIndex: number): number[] { let ans = new Array(rowIndex + 1).fill(1); for (let i = 2; i < rowIndex + 1; ++i) { for (let j = i - 1; j > 0; --j) { ans[j] += ans[j - 1]; } } return ans; } ``` ### **C++** ```cpp class Solution { public: vector<int> getRow(int rowIndex) { vector<int> row(rowIndex + 1, 1); for (int i = 2; i < rowIndex + 1; ++i) { for (int j = i - 1; j > 0; --j) { row[j] += row[j - 1]; } } return row; } }; ``` ### **Go** ```go func getRow(rowIndex int) []int { row := make([]int, rowIndex+1) row[0] = 1 for i := 1; i <= rowIndex; i++ { for j := i; j > 0; j-- { row[j] += row[j-1] } } return row } ``` ### **Rust** ```rust impl Solution { pub fn get_row(row_index: i32) -> Vec<i32> { let n = (row_index + 1) as usize; let mut res = vec![1; n]; for i in 2..n { for j in (1..i).rev() { res[j] += res[j - 1]; } } res } } ``` ### **...** ``` ``` <!-- tabs:end -->