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"].
class Solution { public: string format(int start,int end) { char buf[40]; if(start==end){ sprintf(buf,"%d",start); }else{ sprintf(buf,"%d->%d",start,end); } return string(buf); } vector<string> summaryRanges(vector<int>& nums) { int numsSize = nums.size(); int start = -1; int end = -1; vector<string> res; for(int i=0;i<numsSize;i++){ if(i==0){ start = nums[i]; }else{ if(nums[i]!=nums[i-1]+1){ end = nums[i-1]; res.push_back(format(start,end)); start = nums[i]; } } if(i==numsSize-1){ end = nums[i]; res.push_back(format(start,end)); } } return res; } };