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]
]
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<vector<int> > generate(int numRows) {
vector<vector<int>> Result;
if (numRows == 0)
return Result;
vector<int> firstrow(1, 1);
Result.push_back(firstrow);
for (int i = 1; i != numRows;++i)
{
vector<int> Irows(i + 1, 1);
for (int j = 1; j != i;++j)
Irows[j] = Result.back()[j - 1] + Result.back()[j];
Result.push_back(Irows);
}
return Result;
}
本文介绍了一种生成帕斯卡三角形的算法实现。通过使用C++编程语言,该算法能够根据输入的行数,生成指定行数的帕斯卡三角形。帕斯卡三角形是一种数学上的三角形排列,每一行的两端为1,中间的每个数字等于上一行相邻两个数字之和。
7893

被折叠的 条评论
为什么被折叠?



