class Solution {
public int longestConsecutive(int[] nums) {
int res = 0;
HashSet<Integer> hashSet = new HashSet<>();
for (int num : nums) {
hashSet.add(num);
}
for (int num : nums) {
if (hashSet.contains(num) && !hashSet.contains(num - 1)) {
int x = num;
int y = x + 1;
while (hashSet.contains(y)) {
hashSet.remove(y);
y ++;
}
res = Math.max(res, y - x);
}
}
return res;
}
}
最长连续序列
最新推荐文章于 2024-07-23 14:22:24 发布