pascals-triangle-ii leetcode C++

Given an index k, return the k th 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?

C++

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> dp;
        dp.push_back(1);
        for(int i = 1; i <= rowIndex;i++){
            vector<int> tmp;
            tmp.push_back(1);
            for(int j = 1; j < i; j++)
                tmp.push_back(dp[j]+ dp[j-1]);
            tmp.push_back(1);
            dp = tmp;
        }
        return dp;
    }
};

 

posted @ 2018-09-22 10:58  vercont  阅读(81)  评论(0编辑  收藏  举报