Concise JavaScript solution


  • 0
    var summaryRanges = function(nums) {
        const res = [];
        for (let start = 0, i = 1; i <= nums.length; i++) {
            if (nums[i] - nums[i - 1] > 1 || i === nums.length) {
                res.push(nums[start] + (start < i - 1 ? '->' + nums[i - 1] : ''));
                start = i;
            }
        }
        return res;
    };
    

Log in to reply
 

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