[Leetcode]Pascal's Triangle II

来源:互联网 发布:电脑语音同声翻译软件 编辑:程序博客网 时间:2024/06/15 16:38

//similar with the last one.

class Solution {

public:
    vector<int> getRow(int rowIndex) {
        vector<int> result;
        vector<int> last;
        result.push_back(1);
        if(rowIndex==0)
        return result;
        result.push_back(1);
        if(rowIndex==1)
        return result;
        for(int i=2;i<=rowIndex;i++)
        {
            last=result;
            result.clear();
            for(int j=1;j<=i+1;j++)
            {
                if(j==1||j==i+1)
                result.push_back(1);
                else
                result.push_back(last[j-1]+last[j-2]);
            }
        }
        return result;
    }
};
0 0
原创粉丝点击