Straight-forward Two Pointers, beats 92%


  • 0
    Z
    public int findLHS(int[] nums) {
        Arrays.sort(nums);
        int max = 0;
        int start;
        int end = 0;
        for (start = 0; start < nums.length && end < nums.length; start++) {
            while (end < nums.length && nums[end] - nums[start] <= 1) {
                end++;
            }
            if (nums[end - 1] == nums[start] + 1) {
               max = Math.max(max, end - start);
            }   
        }
        
        return max;
    }

Log in to reply
 

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