- #include
#include
#include
using namespace std;
vector SummaryRanges(int intarr[], int length)
{
vector vecstr;
string res = “”;
int start = intarr[0];
int end = intarr[0];
for (int i = 1; i < length; i++)
{
if (intarr[i] == (end + 1))
{
end = intarr[i];
}
else
{
if (start == end)
{
res = to_string(start);
vecstr.push_back(res);
start = intarr[i];
end = intarr[i];
res = “”;
}
else
{
res = res + to_string(start);
res = res + “->”;
res = res + to_string(end);
start = intarr[i];
end = intarr[i];
vecstr.push_back(res);
res = “”;
}
}
}
if (start == end)
{
res = to_string(start);
vecstr.push_back(res);
}
else
{
res = res + to_string(start);
res = res + “->”;
res = res + to_string(end);
vecstr.push_back(res);
}
return vecstr;
}
int main()
{
int intarr[7]{ 0,2,3,4,6,8,9 };
vector vecstr = SummaryRanges(intarr, 7);
system(“pause”);
return 0;
}
[LeetCode] Summary Ranges 总结区间
最新推荐文章于 2021-02-13 11:48:21 发布