1- //Solution1: HashMap T: O(n) S: O(n)
1+ // Solution1: HashSet T: O(n) S: O(n)
22class Solution {
33 public int longestConsecutive (int [] nums ) {
4+ if (nums == null || nums .length == 0 ) return 0 ;
5+ Set <Integer > set = new HashSet <>();
6+ // 去重
7+ for (int n : nums ) {
8+ set .add (n );
9+ }
410 int res = 0 ;
5- HashMap <Integer , Integer > map = new HashMap <>();
6- for (int n : nums ) {
7- if (!map .containsKey (n )) {
8- int left = (map .containsKey (n -1 )) ? map .get (n -1 ) : 0 ;
9- int right = (map .containsKey (n +1 )) ? map .get (n +1 ) : 0 ;
10- int sum = left + right + 1 ;
11- map .put (n , sum );
12-
13- res = Math .max (res , sum );
14-
15- map .put (n -left , sum );
16- map .put (n +right , sum );
11+ for (int n : nums ) {
12+ if (set .contains (n -1 )) continue ;
13+ int tmpLen = 1 ;
14+ int cur = n + 1 ;
15+ while (set .contains (cur )) {
16+ tmpLen ++;
17+ cur ++;
1718 }
19+ res = Math .max (res , tmpLen );
1820 }
1921 return res ;
2022 }
2123}
2224
23- //Solution2: HashSet T: O(n) S: O(n)
24- class Solution {
25- public int longestConsecutive (int [] nums ) {
26- Set <Integer > set = new HashSet <>();
27- for (int n : nums ) {
28- set .add (n );
25+ // Solution2: union find 近似 T: O(n) S: O(n)
26+ class UnionFind {
27+ int [] parent ;
28+ int [] rank ;
29+ int maxRank ;
30+
31+ UnionFind (int n ) {
32+ parent = new int [n ];
33+ rank = new int [n ];
34+ for (int i = 0 ; i < n ; i ++) {
35+ parent [i ] = i ;
36+ rank [i ] = 1 ;
2937 }
30-
31- int res = 0 ;
32- for (int n : set ) {
33- if (!set .contains (n -1 )) {
34- int m = n + 1 ;
35- while (set .contains (m )) {
36- m ++;
37- }
38- res = Math .max (res , m - n );
39- }
38+ maxRank = 1 ;
39+ }
40+
41+ int find (int x ) {
42+ while (x != parent [x ]) {
43+ parent [x ] = parent [parent [x ]];
44+ x = parent [x ];
45+ }
46+ return x ;
47+ }
48+
49+ void union (int p , int q ) {
50+ int rootp = find (p );
51+ int rootq = find (q );
52+ if (rootp == rootq ) return ;
53+ if (rank [rootp ] > rank [rootq ]) {
54+ parent [rootq ] = rootp ;
55+ rank [rootp ] += rank [rootq ];
56+ maxRank = Math .max (maxRank , rank [rootp ]);
57+ }
58+ else {
59+ parent [rootp ] = rootq ;
60+ rank [rootq ] += rank [rootp ];
61+ maxRank = Math .max (maxRank , rank [rootq ]);
4062 }
41- return res ;
4263 }
4364}
4465
45- //Solution3: union find T: O(n) S: O(n)
4666class Solution {
4767 public int longestConsecutive (int [] nums ) {
48- int length = nums .length ;
49- UnionFind uf = new UnionFind (length );
50- HashMap <Integer , Integer > map = new HashMap <>();
51- for (int i = 0 ; i < length ; i ++) {
52- if (!map .containsKey (nums [i ])) {
53- map .put (nums [i ], i );
54- if (map .containsKey (nums [i ]-1 )) {
55- uf .union (i , map .get (nums [i ]-1 ));
56- }
57- if (map .containsKey (nums [i ]+1 )) {
58- uf .union (i , map .get (nums [i ]+1 ));
59- }
68+ if (nums == null || nums .length == 0 ) return 0 ;
69+ Map <Integer , Integer > map = new HashMap <>();
70+ UnionFind uf = new UnionFind (nums .length );
71+ // 建立映射,去重
72+ for (int i = 0 ; i < nums .length ; i ++) {
73+ if (!map .containsKey (nums [i ])) map .put (nums [i ], i );
74+ }
75+ for (int n : nums ) {
76+ if (map .containsKey (n +1 )) {
77+ uf .union (map .get (n ), map .get (n +1 ));
6078 }
6179 }
62- return uf .maxUnion ();
63-
80+ return uf .maxRank ;
6481 }
6582}
6683
67- class UnionFind {
68- int [] father ;
69-
70- UnionFind (int length ) {
71- father = new int [length ];
72- for (int i = 0 ; i < length ; i ++) {
73- father [i ] = i ;
74- }
75- }
76-
77- public int maxUnion () {
78- int [] count = new int [father .length ];
84+
85+
86+ //Solution: 不太好理解 HashMap T: O(n) S: O(n)
87+ class Solution {
88+ public int longestConsecutive (int [] nums ) {
7989 int res = 0 ;
80- for (int i = 0 ; i < father .length ; i ++) {
81- count [find (i )]++;
82- res = Math .max (res , count [father [i ]]);
90+ HashMap <Integer , Integer > map = new HashMap <>();
91+ for (int n : nums ) {
92+ if (!map .containsKey (n )) {
93+ int left = (map .containsKey (n -1 )) ? map .get (n -1 ) : 0 ;
94+ int right = (map .containsKey (n +1 )) ? map .get (n +1 ) : 0 ;
95+ int sum = left + right + 1 ;
96+ map .put (n , sum );
97+
98+ res = Math .max (res , sum );
99+
100+ map .put (n -left , sum );
101+ map .put (n +right , sum );
102+ }
83103 }
84104 return res ;
85105 }
86-
87- public int find (int node ) {
88- if (node == father [node ]) return father [node ];
89- father [node ] = find (father [node ]);
90- return father [node ];
91- }
92-
93- public void union (int node1 , int node2 ) {
94- int find1 = find (node1 );
95- int find2 = find (node2 );
96- if (find1 != find2 ) {
97- father [find2 ] = find1 ;
98- }
99- }
100- }
106+ }
107+
0 commit comments