leetcode 118 Pascal's Triangle

来源:互联网 发布:淘宝异地客服兼职 编辑:程序博客网 时间:2024/05/21 09:21

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

Subscribe to see which companies asked this questio

class Solution {public:vector<vector<int>> generate(int numRows) {vector<vector<int>> res;vector<int> vec;if(numRows <= 0) return res;for(int i=1; i <= numRows; i++) {vec.push_back(1);for(int j = 1; i>2 && j < res[i-1-1].size(); j++) {vec.push_back(res[i-2][j]+res[i-2][j-1]);}if(i > 1)vec.push_back(1);res.push_back(vec);vec.clear();}return res;}}




0 0
原创粉丝点击