119. 杨辉三角2 Pascal's Triangle II

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)

  1. public class Solution {
  2. public IList<int> GetRow(int rowIndex) {
  3. List<int> result = new List<int>();
  4. if (rowIndex >= 0) { result.Add(1); }
  5. if (rowIndex >= 1) { result.Add(1); }
  6. for (int index = 2; index <= rowIndex; index++) {
  7. result.Add(1);
  8. for (int i = index - 1; i >= 1; i--) {
  9. result[i] = result[i] + result[i - 1];
  10. }
  11. }
  12. return result;
  13. }
  14. }






posted @ 2017-03-01 23:40  xiejunzhao  阅读(83)  评论(0编辑  收藏  举报