-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode128.java
More file actions
31 lines (29 loc) · 768 Bytes
/
leetcode128.java
File metadata and controls
31 lines (29 loc) · 768 Bytes
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
import java.util.Arrays;
public class leetcode128 {
public static int longestConsecutive(int[] nums) {
int length = 1;
int max = 1;
if(nums.length==0){
return 0;
}
Arrays.sort(nums);
for(int i = 0; i< nums.length-1; i++){
if(nums[i+1]==nums[i]){
continue;
}
if(nums[i+1]==nums[i]+1){
length+=1;
}
max = Math.max(length,max);
if(nums[i+1]!=nums[i]+1){
length=1;
}
}
return max;
}
public static void main(String[] args) {
int[]nums = {1,0,1,2};
int solution = longestConsecutive(nums);
System.out.println(solution);
}
}