Simple JavaScript Solution


  • 0
    /**
     * @param {number[]} nums
     * @return {number}
     */
    var findUnsortedSubarray = function (nums) {
      let targetArr = new Array(...nums).sort((a, b) => a - b);
      let start = 0;
      let end = nums.length - 1;
      while(nums[start] === targetArr[start] && start < nums.length) {
        ++start;
      }
      if (start === nums.length) return 0;
      while (end >= start && nums[end] === targetArr[end] ) {
        --end;
      }
      if (end === start) return 0;
      return end - start + 1;
    };

Log in to reply
 

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