Pascal's Triangle II Leetcode

来源:互联网 发布:邓伦 知乎 编辑:程序博客网 时间:2024/06/03 18:11

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?

 代码:

     vector<int>First(2, 1);     if(rowIndex==0) return vector<int>(1,1);     if(rowIndex==1) return First;     vector<int>lastLine = First;     vector<int>oneLine;     for (int i = 2; i <= rowIndex; i++){         oneLine.clear();         oneLine.push_back(1);     for (int j = 0; j<lastLine.size()-1; j++){         oneLine.push_back(lastLine[j] + lastLine[j + 1]);     }          oneLine.push_back(1);         lastLine = oneLine;     }         return oneLine;    }

0 0