题目描述
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
在杨辉三角中,每个数是它左上方和右上方的数的和。
示例:
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/pascals-triangle
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
结果
其实也就是找规律,很容易找到最后一个公式
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res;
if(numRows < 1) return res;
for(int i = 0; i < numRows; i++)
{
vector<int> temp;
for(int j = 0; j <= i; j++)
{
if(0 == j || i == j)
temp.push_back(1);
else
temp.push_back(res[i-1][j] + res[i-1][j-1]);
}
res.push_back(temp);
}
return res;
}
};