Skip to content

Commit 918bba5

Browse files
committed
feat: longest Consecutive Sequence solution
1 parent e68bd88 commit 918bba5

File tree

1 file changed

+20
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)