forked from lizeyang18/byteDanceAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest18.java
More file actions
43 lines (39 loc) · 1.22 KB
/
test18.java
File metadata and controls
43 lines (39 loc) · 1.22 KB
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
package byteDance;
import java.util.HashMap;
/**
* Created by lizeyang on 2020/5/13.
* 给定一个未排序的整数数组,找出最长连续序列的长度。
* 要求算法的时间复杂度为 O(n)。
*/
public class test18 {
//时间复杂度O(n),就要牺牲空间复杂度了,使用HashMap
public static int test(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
HashMap<Integer, Boolean> map = new HashMap<>();
for (int num : nums) {
map.put(num, false);
}
int maxLen = 0;
for (int num : nums) {
if (!map.get(num)) {
maxLen = Math.max(maxLen, getMaxLen(map, num));
}
}
return maxLen;
}
//以当前数向前和向后依次判断
public static int getMaxLen(HashMap<Integer, Boolean> map, int num) {
if (map.containsKey(num) && !map.get(num)) {
map.put(num, true);
return 1 + getMaxLen(map, num - 1) + getMaxLen(map, num + 1);
} else {
return 0;
}
}
public static void main(String[] args) {
int[] nums = {4, 200, 3, 1, 100, 2, 99, 101, 5};
System.out.println(test(nums));
}
}