Home Leetcode 118 - Pascal's Triangle
Post
Cancel

Leetcode 118 - Pascal's Triangle

Link to problem

Description

Given an integer numRows, return the first numRows of Pascal’s triangle.

Solution

Today’s (2025-08-01) daily problem.

Not really a lot could be say here for creating Pascal’s Triangle.

Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> output(1, {1});

        for (int i = 1; i < numRows; ++i) {
            output.push_back(vector<int>(i+1, 1));
            for (int j = 1; j < i; j++) {
                output[i][j] = output[i-1][j-1] + output[i-1][j];
            }
        }

        return output;
    }
};
This post is licensed under CC BY 4.0 by the author.