首页 > 其他 > 详细

LeetCode 118 Pascal's Triangle

时间:2018-07-16 10:19:38      阅读:178      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/dacc123/p/9315891.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!