forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
37 lines (33 loc) · 985 Bytes
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int len = nums.size();
if(len == 0)return {};
vector<string> ans;
int count = 0;
int idx = 0;
while((idx + count) < len-1){
if(nums[idx+count] == nums[idx+count+1]-1)count++;
else{
string str;
if(count == 0){
str = to_string(nums[idx]);
}
else{
str = to_string(nums[idx])+"->"+to_string(nums[idx+count]);
}
ans.push_back(str);
idx += (count+1);
count = 0;
}
}
//末尾处理
string str;
if(count > 0)
str = to_string(nums[idx])+"->"+to_string(nums[idx+count]);
else
str = to_string(nums[idx]);
ans.push_back(str);
return ans;
}
};