/*
* @lc app=leetcode id=228 lang=cpp
*
* [228] Summary Ranges
*/
// @lc code=start
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int N = nums.size();
vector<string> ans;
if(N <= 0) return ans;
int start = nums[0];
int cnt = 0;
for(int i=1;i<N;i++){
if(nums[i] == nums[i-1]+1){
cnt++;
continue;
}
addStr(ans,start,start+cnt);
start = nums[i];
cnt = 0;
}
addStr(ans,start,start+cnt);
return ans;
}
void addStr(vector<string>& ans,int a,int b){
if(a == b){
ans.push_back(to_string(a));
return ;
}
ans.push_back(to_string(a) + "->" + to_string(b));
}
};
// @lc code=end
No.242 - LeetCode[228] Summary Ranges
最新推荐文章于 2024-09-01 11:38:54 发布