#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
vector<vector<int> > generate(int numRows)
{
vector<vector<int> > v;
for(int i = 0; i < numRows; ++i)
{
if(i == 0)
{
vector<int> v1;
v1.push_back(1);
v.push_back(v1);
}
else
{
vector<int> v1, t = v[i-1];
int len = t.size();
v1.push_back(t[0]);
for(i = 1; i < len; ++i)
{
v1.push_back(t[i-1] + t[i]);
}
v1.push_back(t[len - 1]);
v.push_back(v1);
}
}
return v;
}
void print(vector<vector<int> > v)
{
int len = v.size();
for(int i = 0; i < len; ++i)
{
vector<int> t = v[i];
int n = t.size();
for(int j = 0; j != n; ++j)
{
cout << t[j] << " ";
}
cout << endl;
}
}
};
int main(void)
{
Solution s;
vector<vector<int> >v = s.generate(5);
s.print(v);
return 0;
}
Pascal's Triangle
最新推荐文章于 2024-06-05 14:21:17 发布