LeetCode 118 Pascal's Triangle

Posted Shendu.cc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 118 Pascal's Triangle相关的知识,希望对你有一定的参考价值。

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:

 

c++

class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        
          vector<vector<int> > triangle;
          int j=1;
          for(int i=0;i<numRows;i++)
          {
              vector<int> a;
              for(int k=0;k<j;k++)
              {
                  if(k==0||k==j-1) 
                      a.push_back(1);
                  else
                      a.push_back(triangle[i-1][k]+triangle[i-1][k-1]);
                  
              }
              j++;
              triangle.push_back(a);
          }
        return triangle;
        
    }
};

 


以上是关于LeetCode 118 Pascal's Triangle的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 118 Pascal's Triangle

[Leetcode] 118. Pascal's Triangle

LeetCode练题——118. Pascal's Triangle

LeetCode118 Pascal's Triangle

[leetcode-118]Pascal's triangle 杨辉三角

LeetCode118:Pascal&#39;s Triangle