您好,欢迎来到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;
    }
};

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

Copyright © 2019- 99spj.com 版权所有 湘ICP备2022005869号-5

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务