128. Longest Consecutive Sequence

class Solution {
    public int longestConsecutive(int[] nums) {
        Set<Integer> set=new HashSet<Integer>();
        for(int num:nums)
            set.add(num);
        int res=0;
        for(int num:set)
            if(!set.contains(num-1))
            {
                int x=num+1;
                while(set.contains(x))
                    x++;
                res=Math.max(x-num,res);
            }
        return res;
    }
}

  

posted @ 2017-10-04 05:36  Weiyu Wang  阅读(127)  评论(0编辑  收藏  举报