File tree Expand file tree Collapse file tree 1 file changed +13
-15
lines changed
Expand file tree Collapse file tree 1 file changed +13
-15
lines changed Original file line number Diff line number Diff line change 11class Solution {
22 public int longestConsecutive (int [] nums ) {
3- if (nums == null || nums .length == 0 ) {
3+ if (nums .length == 0 ) {
44 return 0 ;
55 }
66
7- int longestStreak = 0 ;
8- Set < Integer > set = new HashSet <>() ;
7+ Set < Integer > s = new HashSet <>() ;
8+ int result = 0 ;
99
10- for (int n : nums ) {
11- set .add (n );
10+ for (int i = 0 ; i < nums . length ; i ++ ) {
11+ s .add (nums [ i ] );
1212 }
1313
14- for (int n : nums ) {
15- if (!set .contains (n - 1 )) {
16- int currNum = n ;
17- int currStreak = 0 ;
14+ for (int num : nums ) {
15+ if (!s .contains (num - 1 )) {
16+ int start = num ;
1817
19- while (set .contains (currNum )) {
20- ++currNum ;
21- ++currStreak ;
18+ while (s .contains (start + 1 )) {
19+ ++start ;
2220 }
2321
24- longestStreak = Math .max (longestStreak , currStreak );
22+ result = Math .max (result , start - num + 1 );
2523 }
2624 }
2725
28- return longestStreak ;
26+ return result ;
2927 }
30- }
28+ }
You can’t perform that action at this time.
0 commit comments