1

Given a sorted integer array without duplicates, return the summary of its ranges.

For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].

输入一个排序好的整数数组,输出数组中连续数字的范围的数组

/**
 * @param {number[]} nums
 * @return {string[]}
 */
var summaryRanges = function(nums) {
    let rst = [],
        tem = nums[0];
    nums.map((num, i) => {
        if (nums[i+1] - num !== 1) {
           num === tem ? rst.push(`${num}`): rst.push(`${tem}->${num}`);
           tem = nums[i+1]  
        }
    });
    return rst;
};

这是我的解法,不知道有没有有更好更快的实现


你看上去很美味得样子
492 声望22 粉丝

啊?