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> > r;
 5 for(int i = 0; i < numRows; i++){
 6  vector<int> tmp;
 7  for(int j = 0; j <= i; j++){
 8  if(j == 0 || j == i){
 9 tmp.push_back(1);
10  }
11  else{
12  tmp.push_back(r[i - 1][j - 1] + r[i - 1][j]);
13  }
14  }
15  r.push_back(tmp);
16 }
17 return r;
18  }
19 };

 

posted on 2015-04-26 17:22  何氏小萌  阅读(106)  评论(0)    收藏  举报