标签:mamicode
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] ]
class Solution { public: vector<vector<int> > generate(int numRows) { vector<vector<int> > res;//最终结果 vector<int> mem; if(numRows<=0)//参数是0的情况不要忘记 return res; mem.push_back(1); res.push_back(mem); if(numRows==1) return res; mem.push_back(1); res.push_back(mem); if(numRows==2) return res; for(int i=2;i<numRows;i++){ mem.clear(); mem.push_back(1); int len = res[i-1].size(); for(int j=0;j<len-1;j++) mem.push_back(res[i-1][j]+res[i-1][j+1]); mem.push_back(1); res.push_back(mem); } return res; } };
标签:mamicode
原文地址:http://www.cnblogs.com/Xylophone/p/3865102.html