[leetcode][array] Pascal's Triangle II

来源:互联网 发布:js拼接json字符串 编辑:程序博客网 时间:2024/06/05 16:44

题目:

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) {        vector<int> res;        if(rowIndex < 0) return res;        res.push_back(1);        for(int i = 1; i <= rowIndex; ++i){            vector<int> curRow;            for(int j = 0; j <= i; ++j){                if(0 == j || i == j) curRow.push_back(1);                else curRow.push_back(res[j-1]+res[j]);            }            res = curRow;        }        return res;    }};


0 0