99网
您的当前位置:首页119. Pascal‘s Triangle II

119. Pascal‘s Triangle II

来源:99网

Notice that the row index starts from 0.

In Pascal’s triangle, each number is the sum of the two numbers directly above it.

Follow up:

Could you optimize your algorithm to use only O(k) extra space?

Example 1:

Input: rowIndex = 3
Output: [1,3,3,1]
Example 2:

Input: rowIndex = 0
Output: [1]
Example 3:

Input: rowIndex = 1
Output: [1,1]

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int>res(rowIndex+1);
        res[0]=1, res[rowIndex]=1;

        for(int j=0;j<rowIndex;j++)
            for(int i=rowIndex-1;i>0;i--)
                res[i]=res[i-1]+res[i];

        return res;
    }
};

因篇幅问题不能全部显示,请点此查看更多更全内容