The Pascal's Triangle II solutions are below.
15 Jun 2024•Easy•C++ / Python / TypeScript
Pascal's Triangle II
C++, Python and TypeScript solutions for Pascal's Triangle II.
Solution files
Python•pascals-triangle-ii/synced-solution.py
class Solution:
def getRow(self, rowIndex: int) -> List[int]:
row = [1]
for i in range(rowIndex):
row = [1] + [row[j] + row[j + 1] for j in range(len(row) - 1)] + [1]
return row
C++•pascals-triangle-ii/synced-solution.cpp
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> row(rowIndex + 1, 1);
for (int i = 1; i <= rowIndex; i++) {
for (int j = i - 1; j > 0; j--) {
row[j] = row[j] + row[j - 1];
}
}
return row;
}
};
TypeScript•pascals-triangle-ii/synced-solution.ts
function getRow(rowIndex: number): number[] {
const row = new Array(rowIndex + 1).fill(1);
for (let i = 1; i <= rowIndex; i++) {
for (let j = i - 1; j > 0; j--) {
row[j] = row[j] + row[j - 1];
}
}
return row;
}