-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path228.summary-ranges.cpp
More file actions
57 lines (44 loc) · 1.22 KB
/
228.summary-ranges.cpp
File metadata and controls
57 lines (44 loc) · 1.22 KB
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
/*
* @lc app=leetcode id=228 lang=cpp
*
* [228] s Ranges
*/
// @lc code=str
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
// vector<string> res;
// int i=0;
// int n = nums.size();
// while(i<n){
// int str=0,end=0;
// str = nums[i];
// while(i+1<n && nums[i+1]==nums[i])
// end=nums[i];
// if(str==end)
// res.push_back(to_string(str)+"");
// else
// res.push_back(to_string(str)+"->"+to_string(end));
// i++;
// }
// return res;
int str,end,size=nums.size();
vector<string> res;
str=0;
while(str<size)
{
end=str+1;
while(end<size && nums[end] == nums[end-1]+1)
end++;
string s="";
if(end == str+1)
s += to_string(nums[str]);
else
s += to_string(nums[str]) + "->" + to_string(nums[end-1]);
res.push_back(s);
str=end;
}
return res;
}
};
// @lc code=end