题意:给出一组数列,将这些数列转化为区间的形式。
思路:简单模拟,顺序遍历。
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> re;
for(int i = 0; i < nums.size();) {
string temp;
temp += to_string(nums[i]);
int j = 0;
for(j = i + 1; j < nums.size() && nums[j] == nums[j - 1] + 1; ++ j ) {
}
if(j == i + 1) {
re.push_back(temp);
i = j;
continue;
}
else {
temp += '-';
temp += '>';
temp += to_string(nums[j - 1]);
re.push_back(temp);
i = j;
continue;
}
}
return re;
}
};