-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetCode_128_2.java
33 lines (30 loc) · 1 KB
/
LeetCode_128_2.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
import java.util.*;
public class LeetCode_128_2 {
/**
* 利用sort+滑动窗口, sort时间复杂度为O(nlogn), 滑动窗口时间复杂度为O(n), 应该不会超时,(sort超时了, 悲)
*/
class Solution {
public int longestConsecutive(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for (int num : nums)
set.add(num);
List<Integer> list = new LinkedList<>();
for (Integer i : set) {
list.add(i);
}
Collections.sort(list);
System.out.println(list);
int start = 0, end = 0, res = 0;
while (end < list.size()) {
if (end + 1 < list.size() && list.get(end + 1) == list.get(end) + 1) {
end++;
} else {
res = Math.max(res, end - start + 1);
start = end + 1;
end ++;
}
}
return res;
}
}
}