Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1]
.
Note:
Could you optimize your algorithm to use only O(k) extra space?
题意:计算杨辉三角的某一行,要求空间复杂度为O(k)
public class Solution {
public IList<int> GetRow(int rowIndex) {
List<int> result = new List<int>();
if (rowIndex >= 0) { result.Add(1); }
if (rowIndex >= 1) { result.Add(1); }
for (int index = 2; index <= rowIndex; index++) {
result.Add(1);
for (int i = index - 1; i >= 1; i--) {
result[i] = result[i] + result[i - 1];
}
}
return result;
}
}