BEAT 98% EASY DP


  • 0
    K
    public class Solution {
        public int longestConsecutive(int[] nums) {
            if (nums==null||nums.length==0) return 0;
            Arrays.sort(nums);
            int max=1,last=1;
            for (int i=1;i<nums.length;i++){
                if (nums[i]==nums[i-1]) continue;
                if (nums[i]==nums[i-1]+1)
                    last=last+1;
                else 
                    last=1;
                if (max<last) max=last;
            }
             return max;
        }
    }
    

Log in to reply
 

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