Question
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
My C# Solution
public class Solution {
public IList<IList<int>> Generate(int numRows) {
var result = new List<IList<int>>();
IList<int> row = new List<int>();
while(numRows > 0)
{
row = GenNextRow(row);
result.Add(row);
numRows--;
}
return result;
}
private IList<int> GenNextRow(IList<int> row)
{
var nextRow = new List<int>();
nextRow.Add(1);
if (row.Count > 0)
{
for(var i = 0; i < row.Count - 1; i++)
{
var val = row[i] + row[i+1];
nextRow.Add(val);
}
nextRow.Add(1);
}
return nextRow;
}
}