您的位置:首页 > 其它

Pascal's Triangle

2014-03-03 17:13 134 查看
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]
]

模拟由金子塔型换成下三角型会看的清楚一些,注意numRows = 1,2的特殊情况。总体来说简单。


class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int> > re;

for(int i = 1 ; i <= numRows ; i++)
{
vector<int> vec;
vec.push_back(1);
int j;
if(i == 1){
re.push_back(vec);
continue;
}
if(i == 2){
vec.push_back(1);
re.push_back(vec);
continue;
}
for(j = 1 ; j < i-1; j++)vec.push_back(re[i-2][j-1]+re[i-2][j]);
vec.push_back(1);
re.push_back(vec);

}
return re;
}
};


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: