-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestConsecutive.java
60 lines (56 loc) · 1.49 KB
/
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
package com.leetcode;
import java.util.HashSet;
/**
* Created by jamylu on 2017/12/30.
* leetcode128.
* 给定一个数组,返回最长的连续序列的长度
*/
public class LongestConsecutive {
public static void main(String args[]) {
int nums[] = {100, 4, 200, 1, 3, 2, 4, 6, 7, 8, 9, 10};
System.out.println(longest(nums));
}
public static int longest(int[] num) {
/*无法对负数进行处理,申请的数组空间也不好规定
int arr[] = new int[1000];
int count = 0;
int max = 0;
for (int i = 0; i < nums.length; i++) {
arr[nums[i]]++;
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
count++;
} else {
if (count > max) {
max = count;
}
count = 0;
}
}
return max;
*/
//使用set集合
HashSet<Integer> set = new HashSet<>();
for (int i : num) {
set.add(i);
}
int count = 0;
for (int i : num) {
int l = 1;
int tmp = i;
set.remove(i);
while (set.contains(++tmp)) {
l++;
set.remove(tmp);
}
tmp = i;
while (set.contains(--tmp)) {
l++;
set.remove(tmp);
}
count = Math.max(count, l);
}
return count;
}
}