您的位置:首页 > 其它

Pascal's Triangle II

2015-09-22 21:16 204 查看
【题目描述】

Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,

Return
[1,3,3,1]
.

【思路】

和Pascal's Triangle的第二种思路一样,只是这次输出第rowIndex行的值而不是输出所有的。

【代码】

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> array(1,1);
        for(int i=2;i<=rowIndex+1;i++){
            for(int j=i-2;j>0;j--){
                array[j]=array[j]+array[j-1];
            }
            array.push_back(1);
        }
        return array;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: