[LeetCode-118] Pascal's Triangle
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 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if (0 == numRows) { 7 vector<vector<int> > empty_result; 8 return empty_result; 9 } 10 vector<vector<int> > result(1, vector<int>(1, 1)); 11 for (int i = 1; i < numRows; ++i) { 12 vector<int> cur_row; 13 const vector<int>& last_row = result[i - 1]; 14 for (int j = 0; j <= i; ++j) { 15 cur_row.push_back((0 == j ? 0 : last_row[j - 1]) 16 + (i == j ? 0 : last_row[j])); 17 } 18 result.push_back(cur_row); 19 } 20 return result; 21 } 22 };
浙公网安备 33010602011771号