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] ]
模拟由金子塔型换成下三角型会看的清楚一些,注意numRows = 1,2的特殊情况。总体来说简单。
class Solution { public: vector<vector<int> > generate(int numRows) { vector<vector<int> > re; for(int i = 1 ; i <= numRows ; i++) { vector<int> vec; vec.push_back(1); int j; if(i == 1){ re.push_back(vec); continue; } if(i == 2){ vec.push_back(1); re.push_back(vec); continue; } for(j = 1 ; j < i-1; j++)vec.push_back(re[i-2][j-1]+re[i-2][j]); vec.push_back(1); re.push_back(vec); } return re; } };