CoderJesse  
wangjiexi@CS.PKU
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         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         int *a = new int[numRows];
 7         vector<vector<int> > v;
 8         vector<int> *R[numRows];
 9         for(int i = 1;i <= numRows;i++)
10         {
11             for(int j = i - 1;j >= 0;j--)
12             {
13                 if(j == 0)
14                     a[j] = 1;
15                 else if(j == i-1)
16                     a[j] = 1;
17                 else
18                     a[j] = a[j]+a[j-1];
19             }
20             R[i-1] = new vector<int>();
21             for(int j = 0;j < i;j++)
22                 R[i-1]->push_back(a[j]);
23             v.push_back(*R[i-1]);
24         }
25         return v;
26     }
27 };

 

posted on 2013-02-28 12:24  CoderJesse  阅读(119)  评论(0)    收藏  举报