-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRange_extraction.cpp
82 lines (67 loc) · 1.39 KB
/
Range_extraction.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <array>
#include <functional>
#include <random>
#include <chrono>
#include <thread>
using namespace std;
string range_extraction(vector<int> v)
{
v.resize(v.size() + 1);
string res = "";
bool isRange = false;
for (int i = 0; i < v.size(); i++)
{
int j = v[i];
if (i<v.size()-1 &&v[i + 1] != j + 1)
{
if (isRange) res += (v[i] == v[i - 2] + 2) ? "-" : ",";
res += to_string(j) + ",";
isRange = false;
}
else if (!isRange)
{
res += to_string(j);
isRange = true;
}
}
res.erase(res.size() - 2, 2);
return res;
}
string range_extraction2(vector<int> v)
{
using Range = std::pair<int, int>;
string res = "";
vector<Range> result;
for (auto p : v)
{
if ( result.empty() || p != result.back().second + 1)
result.push_back({ p,p });
else
++result.back().second;
}
for (auto i : result)
{
if (i.first == i.second)
{
res += to_string(i.first) + ",";
}
else
{
res += to_string(i.first) + ((i.first + 1 == i.second) ? "," : "-") + to_string(i.second) + ",";
}
}
res.pop_back();
return res;
}
int main()
{
cout << range_extraction({ -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20
}) << endl;
cout << range_extraction2({ -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20
}) << endl;
}