class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int n=nums.size(),i;
vector<string> ranges;
if(n==0)
return ranges;
string tmp="";
int cnt=0;
for(i=0;i<n;)
{
if(tmp=="")
{
tmp=to_string(nums[i]);
cnt=1;
}
while(i<n-1&&nums[i]+1==nums[i+1])
{
cnt++;
i++;
}
if(cnt==1)
ranges.push_back(tmp);
else
{
tmp+="->";
tmp+=to_string(nums[i]);
ranges.push_back(tmp);
}
tmp="";
i++;
}
return ranges;
}
};
228. Summary Ranges
最新推荐文章于 2020-12-24 01:11:36 发布