Skip to content

Commit cb61640

Browse files
committed
longest consecutive sequence solved
1 parent 9a24a1f commit cb61640

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
1-
import java.util.HashSet;
2-
import java.util.Set;
3-
41
class Solution {
52
public int longestConsecutive(int[] nums) {
63
Set<Integer> numSet = new HashSet<>();
4+
int max = 0;
75

86
for(int num : nums) {
97
numSet.add(num);
108
}
119

12-
int longestSize = 0;
13-
1410
for(int num : numSet) {
15-
if(!numSet.contains(num - 1)) {
16-
int current = num;
17-
int count = 1;
11+
if(numSet.contains(num - 1)) {
12+
continue;
13+
}
1814

19-
while(numSet.contains(current + 1)) {
20-
count++;
21-
current++;
22-
}
15+
int count = 1;
16+
int curNum = num;
2317

24-
longestSize = Math.max(count, longestSize);
18+
while(numSet.contains(curNum + 1)) {
19+
count++;
20+
curNum++;
2521
}
22+
23+
max = Math.max(max, count);
2624
}
2725

28-
return longestSize;
26+
return max;
2927
}
30-
}
28+
}

0 commit comments

Comments
 (0)