-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSummary Ranges.cpp
64 lines (60 loc) · 1.17 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
#include<iostream>
#include<sstream>
#include<cstring>
#include<vector>
using namespace std;
string inttostring(int num)
{
stringstream ss;
string s;
ss<<num;
ss>>s;
return s;
}
vector<string> summaryRanges(vector<int>& nums)
{
vector<string> re;
int len = nums.size();
string s;
int k = 0;
if (len==0)
return re;
if (len==1)
{
s = inttostring(nums[0]);
re.push_back(s);
return re;
}
s = inttostring(nums[k]);
while (k<len-1)
{
int cont = 0;
while (nums[k] == nums[k+1]-1)
{
k++;
cont++;
}
if (cont>0)
s = s + "->" + inttostring(nums[k]);
k++;
re.push_back(s);
if (k == len)
return re;
s = inttostring(nums[k]);
if (k == len-1)
{
re.push_back(s);
return re;
}
}
}
int main()
{
vector<int> nums;
nums.push_back(0);
nums.push_back(1);nums.push_back(2);nums.push_back(4);nums.push_back(7);
vector<string> re = summaryRanges(nums);
for (int i=0;i<re.size();i++)
cout<<re[i]<<endl;
return 0;
}