# Hard-题目8：128. 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.

public class Solution {
public int longestConsecutive(int[] nums) {
Arrays.sort(nums);
int longest = 1;
int max = 1;
for(int i = 1;i<nums.length;i++) {
if(nums[i]==nums[i-1])
continue;
else if(nums[i]-nums[i-1]==1) {
longest++;
if(longest>max)
max = longest;
}
else
longest=1;
}
return max;
}
}

5ms,beats 95.00%,众数16ms,11.95%
Cmershen的碎碎念：

• 本文已收录于以下专栏：

## leetcode_question_128 Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. ...
• doc_sgl
• 2013年09月20日 14:24
• 1270

## 【LeetCode】128. Longest Consecutive Sequence

128. Longest Consecutive Sequence

## LeetCode 128 Longest Consecutive Sequence

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

## Longest Consecutive Sequence - LeetCode 128

• bu_min
• 2015年05月27日 08:22
• 250

## 154. Find Minimum in Rotated Sorted Array II \ 128. Longest Consecutive Sequence

Find Minimum in Rotated Sorted Array II 题目解析 代码实现154. Find Minimum in Rotated Sorted Array II题目解析Sup...

## LeetCode Algorithms 128. Longest Consecutive Sequence 题解

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

## [LeetCode]128.Longest Consecutive Sequence

Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longes...

## LeetCode: Longest Consecutive Sequence [128]

【题目】 Given an unsorted array of integers, find the length of the longest consecutive elements seque...

举报原因： 您举报文章：Hard-题目8：128. Longest Consecutive Sequence 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)