JS solution beats 97%


  • 0
    L

    /**

    • @param {number[]} nums
    • @return {string[]}
      */
      var summaryRanges = function(nums) {
      var result=[];
      if(!nums.length)
      return result;
      if(nums.length===1){
      result.push(''+nums[0]);
      return result;
      }
      var i=1;
      if(nums[0]+1!==nums[1])
      result.push(nums[0].toString());
      while(i<nums.length){
      var tmp=i;
      while(nums[i]===nums[i-1]+1){
      i++;
      }
      if(tmp!==i){
      result.push(nums[tmp-1]+"->"+nums[i-1]);
      }
      else{
      if(i===nums.length-1)
      result.push(nums[i].toString());
      if(i<nums.length-1&&nums[i+1]!==nums[i]+1)
      result.push(nums[i].toString());
      i++;
      }
      }
      return result;
      };

Log in to reply
 

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