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:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> result;
if(nums.size() == 0)
return result;
string temp = "";
int i=0;
int start = 0;
while(i<nums.size()){
if(temp == ""){
temp = to_string(nums[i]);
start = i;
i++;
}else if(nums[i]-nums[i-1] != 1){
if(start != i-1)
temp += "->" + to_string(nums[i-1]);
result.push_back(temp);
temp = "";
}else{
i++;
}
}
if(temp != ""){
if(start != i-1){
temp += "->" + to_string(nums[i-1]);
}
result.push_back(temp);
}
return result;
}
};