效率O(n*m)
import java.util.HashSet;
import java.util.Set;
public class FindLongestArray {
public static void main(String[] args) {
int[] nums = {1000,4, 200, 1, 3, 2};
System.out.println(help(nums));
}
public static int help(int[] nums){
Set<Integer> sort = new HashSet<Integer>();
int max = 1;
for(int num : nums){
sort.add(num);
}
for(int num: nums){
int left = num - 1;
int right = num + 1;
int count = 1;
while(sort.contains(left)){
count++;
sort.remove(left);
left--;
}
while(sort.contains(right)){
count++;
sort.remove(right);
right++;
}
max = Math.max(count, max);
}
return max;
}
}