Pascal's Triangle

Pascal's Triangle

Not hard

One thing is important. You could not use t[j]=t[j-1]+t[j] to get the the jth element of the array. It was changed by you in the iteration last time.

 1 class Solution {
 2 public:
 3     vector<vector<int> > generate(int numRows) {
 4         vector< vector< int > > result;
 5         result.clear();
 6         if ( 0 == numRows ) {
 7             return result;
 8         }
 9         vector<int > t;
10         t.clear();
11         t.push_back(1);
12         result.push_back(t);
13         int temp,now;
14         for ( int i = 2; i <= numRows; i++ ) {
15             temp = 1;
16             for i( int j = 1; j < i-1; j++ ) {
17                 now = t[j];
18                 t[j] += temp;
19                 temp = now;
20             }
21             t.push_back(1);
22             result.push_back(t);
23         }
24         return result;
25     }
26 };

Loading Disqus comments...
Table of Contents