[118]Pascal's Triangle

来源:互联网 发布:js如何实现随机掉落 编辑:程序博客网 时间:2024/04/30 09:43

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]]

class Solution {public:    vector<vector<int> > generate(int numRows) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        vector<vector<int> > rows;               vector<int> preRow;        vector<int> currRow;                        for(int i = 0; i < numRows; i++)        {            currRow.clear();                        for(int j = 0; j <= i; j++)            {                if(j == i || j == 0)                    currRow.push_back(1);                else                    currRow.push_back(preRow[j-1]+preRow[j]);            }                        rows.push_back(currRow);            preRow = currRow;        }                return rows;    }    };


原创粉丝点击