###### Longest Consecutive Sequence

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:
int longestConsecutive(vector<int>& nums) {
unordered_map<int,bool> used;
for(auto i :nums)
used[i]=false;
int longest=0;

for(auto i:nums)
{
int length=1;
if(used[i]==true)
continue;
used[i]=true;
//在map中找比i大1的连续数，如果没有就返回尾后迭代器，有的
//话，继续找。
for(int j=i+1;used.find(j)!=used.end();j++)
{
used[j]=true;
++length;
}
for(int j=i-1;used.find(j)!=used.end();j--)
{
used[j]=true;
++length;
}
longest=max(length,longest);

}
return longest;
}
};

#### [leetcode] 128. Longest Consecutive Sequence 解题报告

2016-02-25 02:13:43

#### 【LeetCode with Python】 Longest Consecutive Sequence

2014-07-06 15:09:23

#### LeetCode 128. Longest Consecutive Sequence

2016-03-24 23:28:31

#### Longest Consecutive Sequence -- LeetCode

2014-04-05 00:11:16

#### [python]leetcode(128). Longest Consecutive Sequence

2017-09-10 21:53:52

#### [LeetCode]128.Longest Consecutive Sequence

2014-01-17 22:16:21

#### leetcode刷题系列C++-Longest Consecutive Sequence

2016-02-17 14:12:16

#### leetcode 298: Binary Tree Longest Consecutive Sequence

2015-10-29 16:35:32

#### leetcode_c++： Longest Consecutive Sequence（128）

2016-06-11 22:30:39

#### 128. Longest Consecutive Sequence (哈希表结构的使用)

2017-06-05 20:33:15

## 不良信息举报

Longest Consecutive Sequence