228. 汇总区间
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6e2aab0d28f539c072c9398b8f86c40d.png)
方法一
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int numsLength = nums.size(), start = 0, end = 0;
vector<string> result;
while(end < numsLength){
if(end + 1 < numsLength && nums[end + 1] == nums[end] + 1){
end++;
}else{
if(start == end){
result.push_back(to_string(nums[start]));
}else{
result.push_back(to_string(nums[start]) + "->" + to_string(nums[end]));
}
start = end + 1;
end = start;
}
}
return result;
}
};
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int numsLength = nums.size(), i = 0;
vector<string> result;
while(i < numsLength){
int start = i;
while(i + 1 < numsLength && nums[i + 1] == nums[i] + 1){
i++;
}
if(i == start){
result.push_back(to_string(nums[start]));
}else{
result.push_back(to_string(nums[start]) + "->" + to_string(nums[i]));
}
i++;
}
return result;
}
};
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int numsLength = nums.size(), start = 0;
vector<string> result;
for(int end = 0; end < numsLength; end++){
if(end + 1 == numsLength || nums[end + 1] != nums[end] + 1){
if(start == end){
result.push_back(to_string(nums[start]));
}else{
result.push_back(to_string(nums[start]) + "->" + to_string(nums[end]));
}
start = end + 1;
}
}
return result;
}
};
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> ret;
int i = 0;
int n = nums.size();
while (i < n) {
int low = i;
i++;
while (i < n && nums[i] == nums[i - 1] + 1) {
i++;
}
int high = i - 1;
string temp = to_string(nums[low]);
if (low < high) {
temp.append("->");
temp.append(to_string(nums[high]));
}
ret.push_back(move(temp));
}
return ret;
}
};