链接:
228. 汇总区间
题意:
升序数组找连续区间
解:
简单遍历题
实际代码:
#include<bits/stdc++.h>
using namespace std;
vector<string> summaryRanges(vector<int>& nums)
{
if(!nums.size()) return {};
int a=INT_MAX,b=INT_MAX;
vector<string>ans;
for(auto num:nums)
{
if(a==INT_MAX||b==INT_MAX) a=b=num;
else
{
if(num==b+1) b++;
else
{
if(a==b) ans.push_back(to_string(a));
else ans.push_back(to_string(a)+"->"+to_string(b));
a=b=num;
}
}
}
if(a==b) ans.push_back(to_string(a));
else ans.push_back(to_string(a)+"->"+to_string(b));
return ans;
}
int main()
{
return 0;
}
限制:
0 <= nums.length <= 20
-231 <= nums[i] <= 231 - 1
nums
中的所有值都 互不相同nums
按升序排列