我是靠谱客的博主 鳗鱼背包,这篇文章主要介绍Java最长连续序列,现在分享给大家,希望可以做个参考。

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最长连续序列内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(110)

评论列表共有 0 条评论

立即
投稿
返回
顶部