您的位置:首页 > 其它

Pascal's Triangle

2015-05-26 10:07 169 查看
题目描述:

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

For example,

Given
[100, 4, 200, 1, 3, 2]
,

The longest consecutive elements sequence is
[1, 2, 3, 4]
. Return its length:
4
.

Your algorithm should run in O(n) complexity.

代码:

class Solution
{
public:
vector<vector<int>> generate(int numRows)
{
vector<vector<int>> result;
vector<int> line1 = {1};
vector<int> line2 = {1,1};

if(numRows == 0)
return result;
result.push_back(line1);
if(numRows == 1)
return result;
result.push_back(line2);
if(numRows == 2)
return result;

for(int i = 3;i <= numRows;i++)
{
vector<int> temp;
for(int j = 0;j < i;j++)
{
if(j == 0 || j == (i-1))
temp.push_back(1);
else
temp.push_back(result[i-2][j-1]+result[i-2][j]);
}
result.push_back(temp);
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: