求教一个有关Pascal's triangle的一个简单问题,谢谢。。。

csdn19891 csdn19891 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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]
]
对于res[i-2],当i-2<0时,为什么不会报错?
row[j] = res[i-2][j-1] + res[i-2][j]; 是怎么得出的?是归纳的么?是用已知的某一位置的结果,推断列的值?不理解这句话。。。
row[i-1] = 1;表示i-1行都为1,肯定不是啊,没太明白程序的意思。。。
但这个程序通过了。。。
谢谢。

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;
for(int i = 1; i<= numRows;++i){
vector<int> row(i);
row[0] = 1; //
row[i-1] = 1; //
for(int j = 1; j< i-1; ++j){
row[j] = res[i-2][j-1] + res[i-2][j]; //
}
res.push_back(row);
}
return res;
}
};
...全文
评论 6
收藏 已收藏
分享
返回顶部
csdn19891
C++ 语言 65,212 成员 250,513 内容