求教一个有关Pascal's triangle的一个简单问题,谢谢。。。
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,肯定不是啊,没太明白程序的意思。。。
但这个程序通过了。。。
谢谢。
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;
}
};
...全文


