###### 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) {
sort(nums.begin(), nums.end());
int temp = 1, max_len = 1;
for(int i = 1; i < nums.size(); ++i){
if(nums[i] == nums[i - 1])
continue;
else if(nums[i - 1] + 1 == nums[i]){
temp++;
}else{
if(temp >= max_len){
max_len = temp;
}
temp = 1;
}
}
if(temp > max_len){
max_len = temp;
}
return max_len;
}
};

#### [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

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

2015-10-29 16:35:32

#### [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_c++： Longest Consecutive Sequence（128）

2016-06-11 22:30:39

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

2017-06-05 20:33:15

## 不良信息举报

Longest Consecutive Sequence