# C#LeetCode刷题之#118-杨辉三角（Pascal’s Triangle）

[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.

In Pascal’s triangle, each number is the sum of the two numbers directly above it.

Input: 5

Output:

[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

```public class Program {

public static void Main(string[] args) {
var res = Generate(5);

ShowArray(res);

}

private static void ShowArray(IList<IList<int>> array) {
foreach(var num in array) {
foreach(var num2 in num) {
Console.Write(\$"{num2} ");
}
Console.WriteLine();
}
Console.WriteLine();
}

private static IList<IList<int>> Generate(int numRows) {
if(numRows == 0) {
return new int[][] { };
}
int[][] res = new int[numRows][];
for(int i = 0; i < res.Length; i++) {
res[i] = new int[i + 1];
}
res[0][0] = 1;
for(int i = 1; i < numRows; i++) {
res[i][0] = 1;
for(int j = 1; j < i + 1; j++) {
if(j >= i) {
res[i][j] = res[i - 1][j - 1];
} else {
res[i][j] = res[i - 1][j - 1] + res[i - 1][j];
}
}
}
return res;
}

}```

```1
1 1
1 2 1
1 3 3 1
1 4 6 4 1```