-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestConsSequence.java
48 lines (44 loc) · 1.18 KB
/
LongestConsSequence.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
package leetcode;
import java.util.HashMap;
/**
* 返回无序数组中的最长的连续递增元素,要求时间复杂度为O(n)
* Given[100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.
* @author HJH
* 遇到不能排序,又具有时间复杂度要求的问题的有序问题时候,只能增加空间复杂度,用hashmap或hashset解决问题
*
*/
public class LongestConsSequence {
public int longestConsecutive(int[] num) {
HashMap<Integer, Integer> hashMap = new HashMap<>();
for(int i:num){
hashMap.put(i, 1);
}
int maxLength = 0;
for(int i:num){
int index = i;
int length = 0;
//向后遍历
while(hashMap.containsKey(index) && hashMap.get(index)!=-1){
hashMap.put(index, -1);
length++;
index++;
}
maxLength = Math.max(maxLength, length);
//向前遍历
index = i-1;
while(hashMap.containsKey(index) && hashMap.get(index)!=-1){
hashMap.put(index, -1);
length++;
index--;
}
maxLength = Math.max(maxLength, length);
}
return maxLength;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] num = {100, 4, 200, 1, 3, 2};
System.out.println(new LongestConsSequence().longestConsecutive(num));
}
}