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