-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path228. Summary Ranges
55 lines (46 loc) · 1.08 KB
/
228. Summary Ranges
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
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums)
{
int n = nums.size();
vector<string> ans;
if(nums.size()==0) return ans;
int a=0;
string temp;
for(int i=1; i<n; i++)
{
if(nums[i-1]+1 == nums[i])
{
if(a==0)
temp+=to_string(nums[i-1]);
a=1;
}
else
{
if(a==1)
{
temp+='-';
temp+='>';
temp+=to_string(nums[i-1]);
}
else
temp+=to_string(nums[i-1]);
a=0;
ans.push_back(temp);
temp="";
}
}
if(temp.size()>0 && nums[n-1] != stoi(temp))
{
temp+='-';
temp+='>';
temp+=to_string(nums[n-1]);
}
else
{
temp+=to_string(nums[n-1]);
}
ans.push_back(temp);
return ans;
}
};