java simple solution


  • 0
    P

    My first solved hard question ↖(^ω^)↗

    public class Solution {
        public int longestConsecutive(int[] nums) {
            if(nums.length == 0)
            {
                return 0;
            }
            if(nums.length == 1)
            {
                return 1;
            }
            Arrays.sort(nums);
            int[] length = new int[nums.length];
            int count = 0;
            for(int k = 0;k<length.length;k++)
            {
                length[k] = 1;
            }
            for(int i = 0;i<nums.length - 1;i++)
            {
                if(nums[i+1]-nums[i] == 0)
                {
                    continue;
                }
                if(nums[i+1]-nums[i] == 1||nums[i+1]-nums[i] == -1)
                {
                    length[count]++;
                }
                else
                {
                    count++;
                }
            }
            int max = 1;
            for(int j = 0;j<length.length;j++)
            {
                if(length[j]>max)
                {
                    max = length[j];
                }
            }
            return max;
        }
    }
    

Log in to reply
 

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