Leetcode - Array - 118. Pascal's Triangle(杨辉三角)

来源:互联网 发布:html5 javascript 联系 编辑:程序博客网 时间:2024/05/30 23:04

1. Problem Description

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]

]

 

简单模拟。

 

2. My solution

   vector<vector<int> > generate(int numRows)    {        vector< vector<int> >res;        for(int i=0; i<numRows; i++)        {            vector<int>tmp;            res.push_back(tmp);            res[i].push_back(1);            int numCols=i+1;            for(int j=1; j<numCols-1; j++)            {                int num=res[i-1][j-1]+res[i-1][j];                res[i].push_back(num);            }            if(i!=0)                res[i].push_back(1);        }        return res;    }


0 0
原创粉丝点击