문제 링크 : Summary Ranges
/**
* @param {number[]} nums
* @return {string[]}
*/
var summaryRanges = function(nums) {
if(nums.length === 1) return [`${nums[0]}`]
if(nums.length === 0) return []
let chk = [nums[0], nums[0]]
let result = []
for(let i=1; i<nums.length; i++) {
if(nums[i] !== chk[1] + 1) {
chk[0] === chk[1] ? result.push(`${chk[0]}`) : result.push(chk.join('->'))
chk = [nums[i], nums[i]]
} else{
chk[1] = nums[i]
}
if(i === nums.length-1) {
chk[0] === chk[1] ? result.push(`${chk[0]}`) : result.push(chk.join('->'))
}
}
return result;
};
/**
* @param {number[]} nums
* @return {string[]}
*/
var summaryRanges = function(nums) {
let res = [];
let start = null;
for (let i = 0; i < nums.length; i++) {
if (start === null)
start = nums[i];
if (nums[i] === nums[i + 1] - 1)
continue;
if (nums[i] === start) {
res.push(`${ start }`);
} else {
res.push(`${ start }->${ nums[i] }`);
}
start = null;
}
return res;
};