118. Pascal’s Triangle

Description:

https://leetcode.com/problems/pascals-triangle/#/description

Code:

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result;
for (int i = 1; i <= numRows;i++)
{
vector <int> row = vector<int>(i, 1);
if (i >= 3)
for (int j = 1; j < i - 1;j++)
row[j] = result[i - 2][j - 1] + result[i - 2][j];
result.push_back(row);
}
return result;
}
};

Timing:

3ms (beats 4.91%)

Leave a Reply

Your email address will not be published. Required fields are marked *