File tree Expand file tree Collapse file tree 3 files changed +60
-45
lines changed Expand file tree Collapse file tree 3 files changed +60
-45
lines changed Original file line number Diff line number Diff line change 66
77// Your algorithm should run in O(n) complexity.
88
9- public class LongestConsecutiveSequence {
9+ class LongestConsecutiveSequence {
1010 public int longestConsecutive (int [] nums ) {
11- int res = 0 ;
12- HashMap <Integer , Integer > map = new HashMap <Integer , Integer >();
11+ if (nums == null || nums .length == 0 ) {
12+ return 0 ;
13+ }
14+
15+ Set <Integer > set = new HashSet <Integer >();
16+ for (int n : nums ) {
17+ set .add (n );
18+ }
1319
14- for (int n : nums ) {
15- if (!map .containsKey (n )) {
16- int left = (map .containsKey (n - 1 )) ? map .get (n - 1 ) : 0 ;
17- int right = (map .containsKey (n + 1 )) ? map .get (n + 1 ) : 0 ;
20+ int maxLength = 0 ;
21+ for (int n : set ) {
22+ if (!set .contains (n - 1 )) {
23+ int current = n ;
24+ int currentMax = 1 ;
1825
19- int sum = left + right + 1 ;
20- map .put (n , sum );
21- res = Math .max (res , sum );
26+ while (set .contains (n + 1 )) {
27+ currentMax ++;
28+ n ++;
29+ }
2230
23- map .put (n - left , sum );
24- map .put (n + right , sum );
25- } else {
26- continue ;
31+ maxLength = Math .max (maxLength , currentMax );
2732 }
2833 }
2934
30- return res ;
35+ return maxLength ;
3136 }
3237}
Original file line number Diff line number Diff line change 66
77// Your algorithm should run in O(n) complexity.
88
9- public class LongestConsecutiveSequence {
9+ class LongestConsecutiveSequence {
1010 public int longestConsecutive (int [] nums ) {
11- int res = 0 ;
12- HashMap <Integer , Integer > map = new HashMap <Integer , Integer >();
11+ if (nums == null || nums .length == 0 ) {
12+ return 0 ;
13+ }
14+
15+ Set <Integer > set = new HashSet <Integer >();
16+ for (int n : nums ) {
17+ set .add (n );
18+ }
1319
14- for (int n : nums ) {
15- if (!map .containsKey (n )) {
16- int left = (map .containsKey (n - 1 )) ? map .get (n - 1 ) : 0 ;
17- int right = (map .containsKey (n + 1 )) ? map .get (n + 1 ) : 0 ;
20+ int maxLength = 0 ;
21+ for (int n : set ) {
22+ if (!set .contains (n - 1 )) {
23+ int current = n ;
24+ int currentMax = 1 ;
1825
19- int sum = left + right + 1 ;
20- map .put (n , sum );
21- res = Math .max (res , sum );
26+ while (set .contains (n + 1 )) {
27+ currentMax ++;
28+ n ++;
29+ }
2230
23- map .put (n - left , sum );
24- map .put (n + right , sum );
25- } else {
26- continue ;
31+ maxLength = Math .max (maxLength , currentMax );
2732 }
2833 }
2934
30- return res ;
35+ return maxLength ;
3136 }
3237}
Original file line number Diff line number Diff line change 66
77// Your algorithm should run in O(n) complexity.
88
9- public class LongestConsecutiveSequence {
9+ class LongestConsecutiveSequence {
1010 public int longestConsecutive (int [] nums ) {
11- int res = 0 ;
12- HashMap <Integer , Integer > map = new HashMap <Integer , Integer >();
11+ if (nums == null || nums .length == 0 ) {
12+ return 0 ;
13+ }
14+
15+ Set <Integer > set = new HashSet <Integer >();
16+ for (int n : nums ) {
17+ set .add (n );
18+ }
1319
14- for (int n : nums ) {
15- if (!map .containsKey (n )) {
16- int left = (map .containsKey (n - 1 )) ? map .get (n - 1 ) : 0 ;
17- int right = (map .containsKey (n + 1 )) ? map .get (n + 1 ) : 0 ;
20+ int maxLength = 0 ;
21+ for (int n : set ) {
22+ if (!set .contains (n - 1 )) {
23+ int current = n ;
24+ int currentMax = 1 ;
1825
19- int sum = left + right + 1 ;
20- map .put (n , sum );
21- res = Math .max (res , sum );
26+ while (set .contains (n + 1 )) {
27+ currentMax ++;
28+ n ++;
29+ }
2230
23- map .put (n - left , sum );
24- map .put (n + right , sum );
25- } else {
26- continue ;
31+ maxLength = Math .max (maxLength , currentMax );
2732 }
2833 }
2934
30- return res ;
35+ return maxLength ;
3136 }
3237}
You can’t perform that action at this time.
0 commit comments