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

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal’s triangle.

Note that the row index starts from 0.

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

Could you optimize your algorithm to use only O(k) extra space?

Input: 3

Output: [1,3,3,1]

public class Program {

public static void Main(string[] args) {
var res = GetRow(4);
var res2 = GetRow2(5);

ShowArray(res);
ShowArray(res2);

}

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

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

private static IList<int> GetRow2(int rowIndex) {
int[] res = new int[rowIndex + 1];
res[0] = 1;
for(int i = 1; i < rowIndex + 1; i++) {
for(int j = rowIndex; j >= 1; j--) {
res[j] = res[j - 1] + res[j];
}
}
return res;
}

}

1 4 6 4 1
1 5 10 10 5 1

GetRow2在最坏的情况下的时间复杂度为：  ，由于使用一维数组空间复杂度为：  。

GetRow2方法可以根据杨辉三角的对称性优化，只需计算一半即可，其实现方法留给各位看官。

(2)