Skip to content

Commit 0d5334c

Browse files
committed
feat: longest consecutive sequence
1 parent 6ae4bbe commit 0d5334c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int longestConsecutive(int[] nums) {
3+
Set<Integer> set = new HashSet<>();
4+
for(int num: nums) {
5+
set.add(num);
6+
}
7+
8+
int answer = 0;
9+
for(int num: nums){
10+
if(set.contains(num-1)){
11+
continue;
12+
}
13+
int length = 1;
14+
while (set.contains(num+length)){
15+
length++;
16+
}
17+
answer = Math.max(answer, length);
18+
}
19+
20+
return answer;
21+
}
22+
}

0 commit comments

Comments
 (0)