leetcode - Pascal's Triangle

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

思路: 比较简单,但是coding的过程中遇到几个比较奇怪的问题。另外感觉自己思考的细节也不是特别简洁。 注意numRow=0;还有就是for语句;还有就是vector<int>

posted @ 2015-06-19 17:51  cnblogshnj  阅读(99)  评论(0)    收藏  举报