Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
Clarification Your algorithm should run in O(n) complexity.
Example
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
Basic Idea:
题目要求在O(n)时间内完成,首先排除了排序之类的相关方法,因为即使使用 counting sort,也需要O(range)的时间。接下来我们就想到了第一次遍历使用一个set存放所有数,第二次的时候从每个连续序列的第一个数(n-1 not in set)开始,测量这个连续序列的长度,最终返回最长长度。
class Solution:
"""
@param: num: A list of integers
@return: An integer
"""
def longestConsecutive(self, num):
st = set(num)
maxLen = 0
for n in num:
if n - 1 not in st:
length = 1
while n + 1 in st:
length += 1
n += 1
maxLen = max(maxLen, length)
return maxLen
public class Solution {
/*
* @param num: A list of integers
* @return: An integer
*/
public int longestConsecutive(int[] num) {
if (num == null || num.length == 0) return 0;
Set<Integer> set = new HashSet<>();
for (int n : num) {
set.add(n);
}
int maxLength = 1;
for (int n : num) {
// 如果当前n是连续序列的第一个数,依次计算n++,看该序列有多长
if (! set.contains(n - 1)) {
int len = 1;
while (set.contains(++n)) {
len += 1;
}
maxLength = Math.max(maxLength, len);
}
}
return maxLength;
}
}
class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) set.add(num);
int ret = 0;
for (int num : nums) {
int curr = num;
int len = 0;
while (set.remove(curr)) {
len++;
curr++;
}
curr = num - 1;
while (set.remove(curr)) {
len++;
curr--;
}
ret = Math.max(len, ret);
}
return ret;
}
}