-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path228.cpp
More file actions
24 lines (24 loc) · 769 Bytes
/
Copy path228.cpp
File metadata and controls
24 lines (24 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
vector<string> summaryRanges(vector<int> &nums) {
int size = nums.size();
if (size == 0)
return {};
vector<string> ret;
int start = nums[0];
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] != nums[i - 1] + 1) {
if (start == nums[i - 1])
ret.push_back(to_string(start));
else
ret.push_back(to_string(start) + "->" + to_string(nums[i - 1]));
start = nums[i];
}
}
if (start == nums[size - 1])
ret.push_back(to_string(start));
else
ret.push_back(to_string(start) + "->" + to_string(nums[size - 1]));
return ret;
}
};