class Solution {
public:
vector<vector<int> > generate(int numRows) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > res;
if (numRows == 0) return res;
vector<int> temp;
temp.push_back(1);
res.push_back(temp);
if (numRows == 1)
return res;
temp.clear();
temp.push_back(1);
temp.push_back(1);
res.push_back(temp);
if (numRows == 2)
return res;
int length = 3;
for (int i = 3; i <= numRows; ++i)
{
temp.clear();
temp.push_back(1);
length = i;
for (int j = 1; j < length - 1; ++j)
{
temp.push_back(res[i - 2][j - 1] + res[i - 2][j]);
}
temp.push_back(1);
res.push_back(temp);
}
return res;
}
};
[Leetcode] Pascal's Triangle
最新推荐文章于 2021-02-16 00:39:16 发布