-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary_ranges.cpp
87 lines (74 loc) · 1.93 KB
/
summary_ranges.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
83
84
85
86
87
// https://leetcode.com/problems/summary-ranges/
// July 26, 2016
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums)
{
vector<string> vecRanges;
if (nums.empty())
{
return vecRanges;
}
int st_range = nums.front();
int en_range = st_range;
for (size_t i = 1; i != nums.size(); ++i)
{
int cur_num = nums[i];
if (cur_num == (en_range+1))
{
en_range = cur_num;
}
else
{
// start of new range
// First add the previous range in output
string str_range = create_range(st_range, en_range);
vecRanges.push_back(str_range);
// Assign the start of new range
st_range = cur_num;
en_range = cur_num;
}
}
// add the last range
string str_range = create_range(st_range, en_range);
vecRanges.push_back(str_range);
return vecRanges;
}
private:
string create_range(int st_range, int en_range)
{
stringstream ss;
ss << st_range;
string str_range = ss.str();
ss.str(string());
if (st_range < en_range)
{
str_range += "->";
ss << en_range;
str_range += ss.str();
ss.str(string());
}
return str_range;
}
};
int main(int argc, char* argv[])
{
vector<int> nums = {0,4,5,7};
Solution sln;
vector<string> summary_ranges = sln.summaryRanges(nums);
for (vector<string>::iterator it = summary_ranges.begin(); it != summary_ranges.end(); ++it)
{
if (it != summary_ranges.begin())
{
cout << ",";
}
cout << *it;
}
cout << endl;
return 0;
}