题意:给出一组数列,将这些数列转化为区间的形式。
思路:简单模拟,顺序遍历。
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;
}
};

本文介绍了一种将数列转化为区间形式的算法实现。通过顺序遍历的方式,使用C++编程语言,该算法能有效地将输入的一组整数序列转换为一系列连续的区间,并用字符串形式返回结果。
378

被折叠的 条评论
为什么被折叠?



