LeetCode OJ: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 public:
 3     vector<vector<int>> generate(int numRows) {
 4         vector<vector<int>> ret;
 5         vector<int> tmpVec;
 6         ret.clear();
 7         tmpVec.clear();
 8         for(int i = 0; i < numRows; ++i){
 9             if(i == 0){
10                 tmpVec.push_back(1);
11             }else{
12                 for(int j = 0; j <= i; ++j){
13                     if(j == 0) tmpVec.push_back(1);
14                     else if(j == i) tmpVec.push_back(1);
15                     else tmpVec.push_back(ret[i - 1][j - 1] + ret[i - 1][j]);
16                 }
17             }
18             ret.push_back(tmpVec);
19             tmpVec.clear();
20         }
21         return ret;
22     }
23 };
原文地址:https://www.cnblogs.com/-wang-cheng/p/4918826.html