标签:
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"].
类似于 Leetcode 38 Count and Say 在末端增加特殊符号标志结束
在循环中如果发现不是连续整数则分情况放入结果数组
var summaryRanges = function(nums) { var t = 0 var ans = [] nums.push(‘#‘) for(var i=1;i<nums.length;i++) if(nums[i]-nums[t] !== i-t){ if(i-t>1) ans.push(nums[t]+‘->‘+(nums[i-1])) else ans.push(nums[t].toString()) t = i } return ans }
标签:
原文地址:http://www.cnblogs.com/lilixu/p/4611564.html