不是很难 但是写错了一点 复杂度不是o(n)
感谢大佬题解
class Solution {
public int longestConsecutive(int[] nums) {
int ans = 0;
Set<Integer> set = new HashSet<>();
for(int num : nums) set.add(num);
for(int x : set){
if(!set.contains(x - 1)){
int y = x;
while(set.contains(y + 1)) y ++;
ans = Math.max(ans, y - x + 1);
}
}
return ans;
}
}