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;
auto start=nums.begin();
while(start != nums.end())
{
auto end=start;
end++;
string tem="";
int x=*start;
itos(tem,x);
while(end != nums.end() && *end - *start == 1){
start++;
end++;
}
if(x == *start)
res.push_back(tem);
else
{
tem+="->";
itos(tem,*start);
res.push_back(tem);
}
start++;
}
return res;
}
void itos(string &str,int k) //主要是如何快速转换成字符串!!!
{
ostringstream oss;//创建一个流
oss<<k;//把值传递如流中
str+=oss.str();
}
};
浙公网安备 33010602011771号