File tree Expand file tree Collapse file tree 1 file changed +13
-15
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 1 file changed +13
-15
lines changed Original file line number Diff line number Diff line change 1
- import java .util .HashSet ;
2
- import java .util .Set ;
3
-
4
1
class Solution {
5
2
public int longestConsecutive (int [] nums ) {
6
3
Set <Integer > numSet = new HashSet <>();
4
+ int max = 0 ;
7
5
8
6
for (int num : nums ) {
9
7
numSet .add (num );
10
8
}
11
9
12
- int longestSize = 0 ;
13
-
14
10
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
+ }
18
14
19
- while (numSet .contains (current + 1 )) {
20
- count ++;
21
- current ++;
22
- }
15
+ int count = 1 ;
16
+ int curNum = num ;
23
17
24
- longestSize = Math .max (count , longestSize );
18
+ while (numSet .contains (curNum + 1 )) {
19
+ count ++;
20
+ curNum ++;
25
21
}
22
+
23
+ max = Math .max (max , count );
26
24
}
27
25
28
- return longestSize ;
26
+ return max ;
29
27
}
30
- }
28
+ }
You can’t perform that action at this time.
0 commit comments