[LeetCode] Pascal's Triangle

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]
]

模拟题。
 1 class Solution {
 2 private:
 3     vector<vector<int> > ret;
 4 public:
 5     vector<vector<int> > generate(int numRows) {
 6         // Start typing your C/C++ solution below
 7         // DO NOT write int main() function
 8         ret.clear();
 9         
10         for(int i = 0; i < numRows; i++)
11         {
12             if (i == 0)
13             {
14                 vector<int> a;
15                 a.push_back(1);
16                 ret.push_back(a);
17             }
18             else
19             {
20                 vector<int> a;
21                 for(int j = 0; j <= i; j++)
22                     if (j == 0)
23                         a.push_back(ret[i-1][j]);
24                     else if (j == i)
25                         a.push_back(ret[i-1][j-1]);
26                     else
27                         a.push_back(ret[i-1][j-1] + ret[i-1][j]);
28                 ret.push_back(a); 
29             }
30         }
31         
32         return ret;
33     }
34 };
原文地址:https://www.cnblogs.com/chkkch/p/2767701.html