Pascal’s Triangle
Problem
Given an integer numRows
, return the first numRows of Pascal’s triangle.
In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:
Constraints
1 <= numRows <= 30
Solution
The problem Pascal's Triangle
can be solved intuitively by allocating the sum of the two numbers directly above to each index in array.
Implementation
class Solution
{
public:
vector<vector<int>> generate(int numRows)
{
vector<vector<int>> ret;
for (int i = 0; i < numRows; i++)
{
vector<int> row(i + 1, 1);
for (int j = 1; j < i; j++)
row[j] = ret[i - 1][j - 1] + ret[i - 1][j];
ret.push_back(row);
}
return ret;
}
};