[LeetCode] 118. Pascal's Triangle

来源:互联网 发布:跳舞机软件下载 编辑:程序博客网 时间:2024/05/20 18:02

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) {        vector<vector<int>> res;        vector<int> row;        for (int i = 0; i < numRows; i++) {            row.push_back(i == 0);            for (int j = i; j > 0; j--)                row[j] += row[j - 1];            res.push_back(row);        }        return res;    }};

这里写图片描述这里写图片描述

原创粉丝点击