[leetcode 118]

来源:互联网 时间:1970-01-01

Question:

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]]

Solution:

<span style="font-size:14px;">class Solution {public: vector<vector<int>> generate(int numRows) { vector<vector<int>> result(numRows); if(numRows == 0) return result; result[0].push_back(1); for(int i = 1; i < numRows; i++){ result[i].push_back(1); for(int j = 0; j < result[i-1].size()-1; j++){ int temp; temp = result[i-1].at(j) + result[i-1].at(j+1); result[i].push_back(temp); } result[i].push_back(1); } return result; }};</span>




相关阅读:
Top