iven 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"].
题意就是连续的一段数字为一个区域,即 nums[j]+1==nums[j+1]
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string>ans;
if(nums.empty())return ans;
int i=0,j=0;
int len=nums.size();
while(j<len){
if(j+1<len&&nums[j]+1==nums[j+1]){
j++;
}else{
string t;
if(i==j)
t= to_string(nums[i]);
else
t= to_string(nums[i])+"->"+ to_string(nums[j]);
ans.push_back(t);
j++;
i=j;
}
}
return ans;
}
};