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"].
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
题解:两个指针 start, end. 如果nums[end+1] = nums[end]+1, 就移动end指针, 否则, 插入字符串nums[start]->nums[end].
/** * @param {number[]} nums * @return {string[]} */ var summaryRanges = function(nums) { var str=new Array(); var start,end,s,e; var len = nums.length; if(len<1 || nums===null){ return str; } str[0]=""; start=0; var k=0; end=0; while(end<len){ str[k]=""; if(end+1<len && nums[end+1]===nums[end]+1){ end++; } else{ if(start === end){ str[k++]+=nums[start]; } else{ str[k++]+=nums[start]+"->"+nums[end]; } end++; start=end; } } return str; };