-
Notifications
You must be signed in to change notification settings - Fork 0
/
200503-1.cpp
54 lines (51 loc) · 1.05 KB
/
200503-1.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// https://leetcode-cn.com/problems/summary-ranges/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> res;
int a = numeric_limits<int>::max();
int b = numeric_limits<int>::min();
for (auto e : nums) {
if (a > b) {
a = b = e;
} else if (b + 1 == e) {
b = e;
} else {
if (a == b) {
res.push_back(to_string(a));
} else {
res.push_back(to_string(a) + "->" + to_string(b));
}
a = b = e;
}
}
if (a < b) {
res.push_back(to_string(a) + "->" + to_string(b));
} else if (a == b) {
res.push_back(to_string(a));
}
return res;
}
};
void print(vector<string>&& a) {
cout << "[ ";
for (auto e : a) cout << "\"" << e << "\" ";
cout << "]" << endl;
}
int main()
{
Solution s;
{
vector<int> a = {0,1,2,4,5,7};
print(s.summaryRanges(a)); // answer: ["0->2","4->5","7"]
}
{
vector<int> a = {0,2,3,4,6,8,9};
print(s.summaryRanges(a)); // answer: ["0","2->4","6","8->9"]
}
return 0;
}