标签:
题目描述:
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); } } };
标签:
原文地址:http://blog.csdn.net/yao_wust/article/details/46004359