-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path139SummaryRanges.cpp
More file actions
43 lines (33 loc) · 848 Bytes
/
Copy path139SummaryRanges.cpp
File metadata and controls
43 lines (33 loc) · 848 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> ans;
int n = nums.size();
for (int i = 0; i < n; i++) {
int start = nums[i];
while (i + 1 < n && nums[i + 1] == nums[i] + 1) {
i++;
}
int end = nums[i];
if (start == end) {
ans.push_back(to_string(start));
} else {
ans.push_back(to_string(start) + "->" + to_string(end));
}
}
return ans;
}
};
int main() {
vector<int> nums = {0, 1, 2, 4, 5, 7};
Solution obj;
vector<string> ans = obj.summaryRanges(nums);
for (string s : ans) {
cout << s << " ";
}
return 0;
}