128. 最长连续序列
class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();
for(int x : nums){
set.add(x);
}
int res = 0;
for(int x : set){
//如果set里不包括x - 1
//说明序列从x开始
if(!set.contains(x - 1)){
int y = x;
while(set.contains(y + 1)) y++;
res = Math.max(res, y - x + 1);
}
}
return res;
}
}
最后
以上就是鳗鱼背包最近收集整理的关于Java最长连续序列的全部内容,更多相关Java最长连续序列内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。

发表评论 取消回复