鏈接:
228. 匯總區(qū)間
題意:
升序數(shù)組找連續(xù)區(qū)間
解:
簡單遍歷題
實際代碼:文章來源:http://www.zghlxwxcb.cn/news/detail-674414.html
#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;
}
限制:文章來源地址http://www.zghlxwxcb.cn/news/detail-674414.html
0 <= nums.length <= 20
-231 <= nums[i] <= 231 - 1
-
nums
中的所有值都 互不相同 -
nums
按升序排列
到了這里,關(guān)于2023-08-26力扣每日一題的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!