[Leetcode]118. Pascal's Triangle

来源:互联网 发布:奥杜尔档案馆数据圆盘 编辑:程序博客网 时间:2024/06/05 02:10

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[     [1],    [1,1],   [1,2,1],  [1,3,3,1], [1,4,6,4,1]]

下一行第一个和最后一个元素初始化为1,其余元素分别等于上一行左上角和右上角元素之和。


class Solution {public:    vector<vector<int>> generate(int numRows) {        vector<vector<int>> result;        vector<int> array;        for (int i = 1; i <= numRows; ++i)        {            for (int j = i -2; j > 0; --j)                array[j] = array[j-1] + array[j];            array.push_back(1);            result.push_back(array);        }        return result;    }};


0 0
原创粉丝点击