LeetCode119 杨辉三角II

题目

给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。
在这里插入图片描述
在杨辉三角中,每个数是它左上方和右上方的数的和。

示例

输入: 3
输出: [1,3,3,1]

C++代码

注意,这里的第0行为1,第1行为1 1。

class Solution {
public:
    vector<int> getRow(int rowIndex) 
    {
        vector<int> nums;
        nums.push_back(1);
        if(rowIndex==0) return nums;
        nums.push_back(1);
        for(int i=1;i<rowIndex;i++)
        {
            getresult(nums,i);
        }    
        return nums;
    }
    void getresult(vector<int> &num,int count)
    {
        for(int i=count;i>=1;i--)   
            num.at(i)=num.at(i-1)+num.at(i);
        num.push_back(1);
    }
};

猜你喜欢

转载自blog.csdn.net/Fang_D/article/details/83107374