Pascal's Triangle II

来源:互联网 发布:cc网络验证3.4 编辑:程序博客网 时间:2024/04/29 08:49

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?

class Solution {public:    vector<int> getRow(int rowIndex) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        if(rowIndex == 0) return vector<int>(1,1);                vector<int> last(1, 1);        vector<int> cur;        for(int i = 1; i <= rowIndex; i++){                        cur.clear();            cur.push_back(1);            for(int j = 1; j < last.size(); j++){                cur.push_back(last[j-1] + last[j]);            }            cur.push_back(1);                        last = cur;                    }                return cur;            }};