Summary Ranges

Given an ordered integer array nums with no duplicate elements.

Return a list of the smallest ordered range intervals that exactly cover all the numbers in the array. In other words, each element of nums is exactly covered by a certain interval range, and there are no numbers x that belong to a certain range but do not belong to nums.

Each interval range [a, b] in the list should be output in the following format:

  • "a->b", if a != b.
  • "a", if a == b.

Example

Input: nums = [0,1,2,4,5,7] Output: ["0->2","4->5","7"] Explanation: Interval ranges are: [0,2] --> "0->2" [4,5] --> "4->5" [7,7] --> "7"
Input: nums = [0,2,3,4,6,8,9] Output: ["0","2->4","6","8->9"] Explanation: Interval ranges are: [0,0] --> "0" [2,4] --> "2->4" [6,6] --> "6" [8,9] --> "8->9"
Input: nums = [] Output: []
Input: nums = [-1] Output: ["-1"]
Input: nums = [0] Output: ["0"]

Solution

/**
 * @param {number[]} nums
 * @return {string[]}
 */
var summaryRanges = function(nums) {
    const n = nums.length;
    if(n === 0) return nums;
    if(n === 1) return [String(nums[0])];
    let left = nums[0];
    let right = nums[0];
    const target = [];
    for (let i=1; i<=n; i++){
        const cur = nums[i];
        const pre = nums[i-1];
        if (cur - pre === 1){
            right = cur;
        }else if(left === right){
            target.push(String(left));
            left = cur;
            right = cur;
        }else{
            target.push([left, "->", right].join(""));
            left = cur;
            right = cur;
        }
    }
    return target;
};

Approach

In this problem, it is especially important to pay attention to the case when two values are equal. Only one value needs to be placed. Below the solution is my previous approach, using an increasing sequence value for comparison with the original sequence value, it needs special handling when two values are equal. The new approach is simpler, using two pointers. If the difference is one, the right pointer moves forward by one. If the difference is greater than one, push an interval into the array, then move both left and right pointers to the next value. First, define the array length. Then, if the length is 0, return directly. If the length is 1, return the value and convert it to a string. Then define the left and right pointers, and define a target array. Establish a loop. In Js, there is no need to worry too much about out-of-bounds situations. When comparing later, it only needs to be treated as undefined. Define the current value and the previous value. If the difference between these two values is 1, then move the right pointer. If the two pointers are equal, convert one of the values to a string and push it into the target array, and set both pointers to the current value. If the difference is not 1 and they are not equal, then push the concatenated string into the array and set both pointers to the current value. After the loop, return the target array.

[https://github.com/WindrunnerMax/EveryDay](https://github.com/WindrunnerMax/EveryDay)

## Reference

[https://leetcode-cn.com/problems/summary-ranges/](https://leetcode-cn.com/problems/summary-ranges/)