# [leetcode]228. Summary Ranges

class Solution {
public:
vector<string> summaryRanges(vector<int>& nums)
{
int n=nums.size();
vector<string> ret;
string s="";

int i=0;int j=0;
while(i<n&&j<n)
{
while(j<n-1 && nums[j+1]-nums[j]==1)
{
j++;
}
if(j+1==n-1)//考虑最后一个数的情形
{
if(nums[j+1]-nums[j]==1)
j+=1;
}

if(j>i)
{
s=ToString(nums[i])+"->"+ToString(nums[j]);
}
else //j==i
{
s=ToString(nums[j]);
}
ret.push_back(s);
i=j+1;
j=i;

}
return ret;
}
string ToString(long long int n)//类型转换函数
{
string str;
if(n<0)//考虑负数的情形
{
str.push_back('-');
n=-n;
}
string str1;
while(1)
{
int b=n%10;
str1.push_back(b+48);//'0'+?
n=n/10;
if(n==0)
break;
}
while(!str1.empty())
{
str.push_back(str1.back());//逆置
str1.pop_back();
}
return str;
}
};

• 本文已收录于以下专栏：

## <LeetCode OJ> 228. Summary Ranges

228. Summary Ranges My Submissions Question Total Accepted: 36554 Total Submissions: 159532 Diff...

## 228. Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, giv...
• iFuMI
• 2016-03-22 17:13
• 138

## [LeetCode] Summary Ranges

Summary Ranges Given a sorted integer array without duplicates, return the summary of its range...

## Leetcode(3)-Summary Ranges

228 Summary Ranges For example, given [0,1,2,4,5,7], return [“0->2”,”4->5”,”7”].class Solution { pu...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)