【LeetCode】 杨辉三角

2019-12-03 15:32:48 浏览数 (1)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/shiliang97/article/details/103186539

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

In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

代码语言:javascript复制
Input: 5
Output:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

基础的题,就是后面的等于(上面 左上)

代码语言:javascript复制
class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> v;
        for(int i=0;i<numRows;i  ){
            vector<int>v2;
            v2.push_back(1);
            for(int l=1;l<i;l  ){
                v2.push_back(v[i-1][l-1] v[i-1][l]);
            }if(i>0)v2.push_back(1);
            v.push_back(v2);
        }return v;
    }
};

0 人点赞