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”].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| /** * @param {number[]} nums * @return {string[]} */ function summaryRanges(nums) { var result =[]; var leng =nums.length; if(leng==0){ return result; } if(leng==1){ return [nums.toString()]; } for(var i=0;i<leng;) { var start=i; var end=i; //当后面一个元素等于前面一个元素 while ((end + 1 < leng) && (nums[end] == nums[end + 1] - 1)){ end++; } if (start < end) { var str = nums[start].toString() + "->" + nums[end].toString(); result.push(str); i = end + 1; } else { var stt = nums[start].toString(); result.push(stt); i = end + 1; } } return result; }
|