Skip to content

Commit 1620167

Browse files
dohee789dohee789
authored andcommitted
👔 longest-consecutive-sequence solution
1 parent 476d86f commit 1620167

File tree

1 file changed

+25
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)