从零开始的LC刷题(29): Pascal's Triangle 帕斯卡三角形(杨辉三角)

原题:

Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.

从零开始的LC刷题(29): Pascal's Triangle 帕斯卡三角形(杨辉三角)_第1张图片
In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 5
Output:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

一行一行求杨辉三角并且都保存在一个二维vector容器中,没什么可说的,结果:

Success

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Pascal's Triangle.

Memory Usage: 8.8 MB, less than 49.32% of C++ online submissions for Pascal's Triangle.

代码:

class Solution {
public:
    vector> generate(int numRows) {
        vector> r;
        if(numRows==0){return r;}
        vector init;
        r.push_back(init);
        r[0].push_back(1);
        for(int i=0;i

 

你可能感兴趣的:(LEETCODE,C++)