-
Notifications
You must be signed in to change notification settings - Fork 0
/
_128_LongestConsecutive.java
87 lines (83 loc) · 2.45 KB
/
_128_LongestConsecutive.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* @author jzy
* @GitHub <a href="https://github.com/567jin">567jin</a>
* @since 2024-03-11 9:40:10
*/
public class _128_LongestConsecutive {
public static void main(String[] args) {
int[] nums = {100, 4, 200, 1, 3, 2};
nums = new int[]{0, -1};
int i = new _128_LongestConsecutive().longestConsecutive(nums);
System.out.println(i);
int i2 = new _128_LongestConsecutive().longestConsecutive2(nums);
System.out.println(i2);
int i3 = new _128_LongestConsecutive().longestConsecutive3(nums);
System.out.println(i3);
}
public int longestConsecutive3(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
Arrays.sort(nums);
int maxLen = Integer.MIN_VALUE;
int n = nums.length;
int len = 1;
for (int i = 1; i < n; i++) {
if (nums[i] == nums[i - 1] + 1) {
len++;
} else if (nums[i] == nums[i - 1]) { // 等于时 直接跳过 不计数 但也不从头计数
} else {
maxLen = Math.max(maxLen, len);
len = 1;
}
}
return Math.max(maxLen, len);
}
/**
* 优化一下解法1的while哈希查找过程
*/
public int longestConsecutive2(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
Set<Integer> set = new HashSet<>();
int maxLen = Integer.MIN_VALUE;
for (int num : nums) {
set.add(num);
}
for (int num : nums) {
if (!set.contains(num - 1)) { // 判断是否之前已经遍历过
int len = 0;
while (set.contains(num++)) {
len++;
}
maxLen = Math.max(len, maxLen);
}
}
return maxLen;
}
/**
* 超时
*/
public int longestConsecutive(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
HashSet<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
int maxLen = Integer.MIN_VALUE;
for (int num : nums) {
int len = 0;
while (set.contains(num++)) {
len++;
}
maxLen = Math.max(len, maxLen);
}
return maxLen;
}
}