LeetCode-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?

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        rowIndex++;
        if(rowIndex<=0)return vector<int>();
        vector<int> ret;
        ret.push_back(1);
        vector<int>temp;
        temp.resize(rowIndex);
        for(int i=1;i<rowIndex;i++){
            for(int j=0;j<i;j++){
                temp[j]=ret[j];
            }
            ret.resize(i+1);
            ret[i]=ret[i-1];
            for(int j=1;j<i;j++){
                ret[j]=temp[j-1]+temp[j];
            }
        }
        return ret;
    }
};
View Code
原文地址:https://www.cnblogs.com/superzrx/p/3352704.html