Very easy with Java(14 Line) beats 73.95% (13 ms)


  • 2
    S
    public int longestConsecutive(int[] nums) {
        Set<Integer> hash=new HashSet();
        for(int i:nums) hash.add(i);
        int max=0,count=0;
        for(int i:nums)
            if(!hash.contains(i-1)){
                count=0;
                while(hash.contains(i++)){
                    count++;
                }
                max=Math.max(max,count);
            }
        return max;
    }

  • 0
    D

    int end=i is unnecessary.


  • 0
    C

    You can set the second for(int i : nums) to for(int i : hash) to avoid duplicates.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.