-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestConsecutiveSequence.java
55 lines (45 loc) · 1.22 KB
/
LongestConsecutiveSequence.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package leetcode;
/*
* 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.
*/
import java.util.*;
public class LongestConsecutiveSequence {
public static int longestConsecutive(int[] nums) {
if (nums.length == 0)
return 0;
HashSet<Integer> set = new HashSet<Integer>();
int max = 1;
for(int e:nums)
set.add(e);
for(int e:nums)
{
int left = e - 1;
int right = e + 1;
int count = 1;
while(set.contains(left))
{
++count;
set.remove(left);
--left;
}
while(set.contains(right))
{
++count;
set.remove(right);
++right;
}
max = Math.max(count, max);
}
return max;
}
public static void main(String[] args)
{
int[] nums = {100,4,200,1,3,2};
int max = longestConsecutive(nums);
System.out.println("max = "+max);
}
}