标签:generate triangle return pascal 分析
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]
]
分析:
代码(c++):
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector <vector<int> > vec(numRows);
for(int i = 0; i < numRows; i++) {
vec[i].resize(i+1);
for(int j = 0; j <= i ; j++){
if(j==i || j==0)
vec[i][j] = 1;
else{
vec[i][j] = vec[i-1][j-1] + vec[i-1][j];
}
}
}
return vec;
}
};
Leetcode[118]-Pascal's Triangle
标签:generate triangle return pascal 分析
原文地址:http://blog.csdn.net/dream_angel_z/article/details/46417769