-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
summaryRanges.cpp
40 lines (33 loc) · 1018 Bytes
/
summaryRanges.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
38
39
40
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int n = nums.size();
vector<string> result;
if(n == 0) return result;
int start = nums[0], end = nums[0];
string val;
for(int i = 1; i < n; ++i) {
if(end + 1 == nums[i]) {
end = nums[i];
continue;
}
if(start == end) {
val = to_string(start);
result.push_back(val);
} else {
val = to_string(start) + "->" + to_string(end);
result.push_back(val);
}
start = nums[i];
end = nums[i];
}
if(start == end) {
val = to_string(start);
result.push_back(val);
} else {
val = to_string(start) + "->" + to_string(end);
result.push_back(val);
}
return result;
}
};