JavaScript Solution


  • 0
    /**
     * @param {number[]} nums
     * @return {number}
     */
    var findLHS = function(nums) {
      nums = nums.sort((a,b) => a-b);
      let start = 0;
      let res = 0;
      let next = 0;
      console.log(nums);
      for(let i = 1; i < nums.length; ++i) {
        if (nums[i] - nums[start] > 1) {
          start = ++next;
        } else if (nums[i] - nums[start] === 1) {
          res = Math.max(i - start +1, res);
        } else {
          next++;
        }
      }
      return res;
    }

Log in to reply
 

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