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> res;
vector<int>::iterator front = nums.begin(), back = nums.begin();
while (back != nums.end())
{
while (back + 1 != nums.end() && *(back + 1) == *back + 1)
++back;
string s;
if (front == back)
s = to_string(*front);
else
s = to_string(*front) + "->" + to_string(*back);
res.push_back(s);
front = back + 1, back = front;
}
return res;
}
};