public class Solution {
public int longestConsecutive(int[] num) {
Set<Integer> set = new HashSet<>();
for (int i: num) {
set.add(i);
}
int longest = 0;
for (int i: num) {
if (set.contains(i)) {
set.remove(i);
int count = 1;
int low = i - 1;
while (set.contains(low)) {
count++;
set.remove(low);
low--;
}
int high = i + 1;
while (set.contains(high)) {
count++;
set.remove(high);
high++;
}
if (count > longest) {
longest = count;
}
}
}
return longest;
}
}
Longest Consecutive Sequence
最新推荐文章于 2022-03-06 23:29:26 发布
