Skip to content

Commit f6bcd2d

Browse files
committed
longest consecutive sequence solved
1 parent c1045c8 commit f6bcd2d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int longestConsecutive(int[] nums) {
5+
Set<Integer> checkList = new HashSet<>();
6+
int seqCnt = 0;
7+
int start = Integer.MIN_VALUE;
8+
9+
for(int n : nums){
10+
checkList.add(n);
11+
}
12+
13+
for (int n : nums) {
14+
int seq = 1;
15+
int target = n+1;
16+
if(checkList.contains(n-1))continue;
17+
18+
while(checkList.contains(target)){
19+
checkList.remove(target);
20+
seq++;
21+
target++;
22+
}
23+
24+
if(seqCnt < seq){
25+
seqCnt = seq;
26+
start = n;
27+
}
28+
}
29+
return seqCnt;
30+
}
31+
}
32+

0 commit comments

Comments
 (0)