simple java code --4ms


  • 0
    G

    public int longestConsecutive(int[] nums) {
    if(nums.length==0)
    return 0;
    Arrays.sort(nums);
    int max_len = 1;
    int num = 1;
    for (int i = 1; i < nums.length; i++) {
    if(nums[i] - nums[i - 1] == 0)
    continue;
    if (nums[i] - nums[i - 1] == 1)
    num += 1;
    else {
    num = 1;
    }
    if (num >= max_len)
    max_len = num;
    }
    return max_len;
    }


Log in to reply
 

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