1
// ��������Please upvote if it helps ��������
2
class Solution {
3
public:
4
vector<string> summaryRanges(vector<int> &nums) {
5
// resultant string
6
vector<string> result;
7

8
int n = nums.size();
9
// if size happens to be zero return empty string
10
if (n == 0) return result;
11

12
// assigning first element to a
13
int a = nums[0];
14

15
for (int i = 0; i < n; i++) {
16
// if one of both is true
17
if (i == n - 1 || nums[i] + 1 != nums[i + 1]) {
18
// if current element is not equals a
19
// this means we have found a range.
20
if (nums[i] != a) result.push_back(to_string(a) + "->" + to_string(nums[i]));
21

22
// this means we have reached to the end of string and now
23
// we have to add a that should be the last element
24
else
25
result.push_back(to_string(a));
26

27
// checking for this condition so that a got updated for next range
28
// also n-1 so that a doesn't contain out of bound value
29
if (i != n - 1) a = nums[i + 1];
30
}
31
}
32
// return result
33
return result;
34
}
35
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0