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> > iveclist;
vector<int> ivec = { 1 }, last = { 1, 1 };
if (numRows <= 0)
return iveclist;
else if (numRows == 1) {
iveclist.push_back(ivec);
return iveclist;
}
else if (numRows == 2) {
iveclist.push_back(ivec);
iveclist.push_back(last);
return iveclist;
}
iveclist.push_back(ivec);
iveclist.push_back(last);
for (int i = 3; i <= numRows; i++) {
ivec.clear();
ivec.push_back(1);
for (int j = 0; j < last.size() - 1; j++)
ivec.push_back(last[j] + last[j + 1]);
ivec.push_back(1);
iveclist.push_back(ivec);
last = ivec;
}
return iveclist;
}
};