118. 杨辉三角
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>>a;
if(numRows==0)
{
return a;
}
if(numRows==1)
{
vector<int>b;
b.push_back(1);
a.push_back(b);
return a;
}
else if(numRows==2)
{
vector<int>b;
b.push_back(1);
a.push_back(b);
b.push_back(1);
a.push_back(b);
return a;
}
vector<int>b;
b.push_back(1);
a.push_back(b);
b.push_back(1);
a.push_back(b);
for(int i=2;i<numRows;i++)
{
b.clear();
b.push_back(1);
for(int j=1;j<=i-1;j++)
{
b.push_back(a[i-1][j-1]+a[i-1][j]);
}
b.push_back(1);
a.push_back(b);
}
return a;
}
};
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> dp(numRows);
if(numRows==0)
return dp;
if(numRows==1)
{
dp[0].push_back(1);
return dp;
}
if(numRows==2)
{
dp[0].push_back(1);
dp[1].push_back(1);dp[1].push_back(1);
return dp;
}
dp[0].push_back(1);
dp[1].push_back(1);dp[1].push_back(1);
for(int i=2;i<numRows;i++)
{
dp[i].push_back(1);
for(int j=1;j<=i-1;j++)
{
dp[i].push_back(dp[i-1][j-1]+dp[i-1][j]);
}
dp[i].push_back(1);
}
return dp;
}
};