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 class Solution { 2 public: 3 vector<string> summaryRanges(vector<int>& nums) { 4 int n=nums.size(); 5 int i; 6 int left,right; 7 char str[100]; 8 left=0; 9 right=0; 10 vector<string> res; 11 if(n<1) 12 return res; 13 for(i=0;i<n;i++) 14 { 15 if(i==0) 16 { 17 left=nums[i]; 18 right=left; 19 } 20 else if(right+1==nums[i]) 21 { 22 right++; 23 } 24 else 25 { 26 if(left!=right) 27 { 28 sprintf(str,"%d->%d",left,right); 29 string tmp(str); 30 res.push_back(tmp); 31 } 32 else 33 { 34 sprintf(str,"%d",left); 35 string tmp(str); 36 res.push_back(tmp); 37 } 38 left=nums[i]; 39 right=left; 40 41 } 42 } 43 if(left!=right) 44 { 45 sprintf(str,"%d->%d",left,right); 46 string tmp(str); 47 res.push_back(tmp); 48 } 49 else 50 { 51 sprintf(str,"%d",left); 52 string tmp(str); 53 res.push_back(tmp); 54 } 55 return res; 56 } 57 };