-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0228-summary-ranges.cpp
57 lines (52 loc) · 1.26 KB
/
0228-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
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int n=nums.size();
if(n==0)
{
return {};
}
vector<string> ans;
int start=0;
int end=0;
for(int i=0;i<n-1;i++)
{
if(nums[i]+1==nums[i+1])
{
end=i+1;
}
else
{
if(start!=end)
{
string x="";
x=to_string(nums[start])+"->"+to_string(nums[end]);
ans.push_back(x);
start=i+1;
end=i+1;
}
else
{
string x="";
x=to_string(nums[start]);
ans.push_back(x);
start=i+1;
end=i+1;
}
}
}
if(start!=end)
{
string x="";
x=to_string(nums[start])+"->"+to_string(nums[end]);
ans.push_back(x);
}
else
{
string x="";
x=to_string(nums[start]);
ans.push_back(x);
}
return ans;
}
};