您的位置:首页 > 编程语言 > Go语言

LeetCode Algorithms #66 <Pascal's Triangle>

2016-04-01 22:12 489 查看
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]
]

思路:

虽然我希望再简单的题我也能记录下自己的思路,但这道题我真的不知道说啥。

解:

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result;
for(int rowIndex = 0; rowIndex < numRows; rowIndex++)
{
vector<int> rowVector;
for(int idx = 0; idx < rowIndex+1; idx++)
{
if(idx == 0 || idx == rowIndex)
{
rowVector.push_back(1);
continue;
}

rowVector.push_back(result[rowIndex-1][idx-1] + result[rowIndex-1][idx]);
}
result.push_back(rowVector);
}
return result;
}

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