LeetCode (Pascal's Triangle)

来源:互联网 发布:淘宝商品布光技巧 编辑:程序博客网 时间:2024/06/14 01:58

Problem:

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

class Solution {public:    vector<vector<int>> generate(int numRows) {        vector<vector<int>> ans;        if(numRows == 0) return ans;        if(numRows == 1) return {{1}};        ans = generate(numRows - 1);        vector<int> tmp, last = ans.back();        tmp.push_back(1);        for(int i = 0; i < last.size() - 1; i++){            tmp.push_back(last[i] + last[i + 1]);        }        tmp.push_back(1);        ans.push_back(tmp);        return ans;    }};


原创粉丝点击