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:

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

 

 

 1 class Solution {
 2 public:
 3     vector<vector<int>> generate(int numRows) {
 4         vector<vector<int>> res ;
 5         if(numRows<=0) return res;
 6         vector<int> t(1,1);
 7         res.push_back(t);
 8         for(int r = 2;r<=numRows;r++){
 9             vector<int> temp(r,1);
10             for(int i = 1;i<r-1;i++)
11                 temp[i] = res[r-2][i-1]+res[r-2][i];
12             res.push_back(temp);
13         }
14         return res;
15     }
16 };

 

posted @ 2019-01-11 13:18  乐乐章  阅读(102)  评论(0编辑  收藏  举报