class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> res;
if(nums.empty()){
return res;
}
int start=nums[0],end=start;
for(int i=1;i<nums.size();i++){
if(nums[i]==end+1){
end+=1;
}
else{
res.push_back(fun(start,end));
start=nums[i],end=start;
}
}
res.push_back(fun(start,end));
return res;
}
string fun(int start,int end){
char tmp[10];
string str;
sprintf(tmp,"%d",start);
str=tmp;
if(start==end){
return str;
}
else{
str+="->";
sprintf(tmp,"%d",end);
string str2=tmp;
return str+str2;
}
}
};
LeetCode - 228 Summary Ranges
最新推荐文章于 2021-01-10 21:07:05 发布