File tree Expand file tree Collapse file tree 2 files changed +15
-14
lines changed
Expand file tree Collapse file tree 2 files changed +15
-14
lines changed Original file line number Diff line number Diff line change @@ -26,18 +26,19 @@ class Solution
2626 {
2727 vector<vector<int > > res;
2828 if (numRows < 1 ) return res;
29+ res.reserve (numRows);
2930 res.push_back (vector<int >(1 , 1 ));
30- while (numRows > 1 )
31+ int m = 1 ;
32+ while (m < numRows)
3133 {
32- size_t m = res.size ();
33- size_t n = res[m-1 ].size ();
3434 vector<int > row;
35+ row.reserve (m+1 );
3536 row.push_back (1 );
36- for (size_t i = 0 ; i < n -1 ; i++)
37+ for (int i = 0 ; i < m -1 ; i++)
3738 row.push_back (res[m-1 ][i] + res[m-1 ][i+1 ]);
3839 row.push_back (1 );
3940 res.push_back (row);
40- numRows-- ;
41+ m++ ;
4142 }
4243 return res;
4344 }
Original file line number Diff line number Diff line change @@ -20,17 +20,17 @@ class Solution
2020 vector<int > getRow (int rowIndex)
2121 {
2222 vector<int > res;
23+ res.reserve (rowIndex+1 );
2324 res.push_back (1 );
24- while (rowIndex > 0 )
25+ if (rowIndex < 1 ) return res;
26+ res.push_back (1 );
27+ int m = 1 ;
28+ while (m < rowIndex)
2529 {
26- vector<int > row;
27- row.push_back (1 );
28- for (size_t i = 0 ; i < res.size ()-1 ; i++)
29- row.push_back (res[i] + res[i+1 ]);
30- row.push_back (1 );
31- res.clear ();
32- res.assign (row.begin (), row.end ());
33- rowIndex--;
30+ for (int i = 0 ; i < m; i++)
31+ res[i] += res[i+1 ];
32+ res.insert (res.begin (), 1 );
33+ m++;
3434 }
3535 return res;
3636 }
You can’t perform that action at this time.
0 commit comments