-
Notifications
You must be signed in to change notification settings - Fork 0
/
MissingRanges.cc
48 lines (35 loc) · 965 Bytes
/
MissingRanges.cc
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
#include "leet.h"
#include <algorithm>
#include <cstring>
class Solution{
public:
void push(vector<string> &ans, int expected, int n) {
if (n == expected + 1) {
ans.push_back(std::to_string(expected));
} else {
ans.push_back(std::to_string(expected) + "->" + std::to_string(n - 1));
}
}
vector<string> findMissingRanges(vector<int> &nums, int lower, int upper) {
vector<string> ans;
string tmp("");
int expected = lower;
for (auto n : nums) {
if (n > upper) break;
if (n != expected) {
push(ans, expected, n);
}
expected = n + 1;
}
if (expected <= upper) {
push(ans, expected, upper + 1);
}
return ans;
}
};
int main(){
Solution slu;
vector<int> a = {0, 1, 3, 50, 75};
cout << slu.findMissingRanges(a, 0, 99) << endl;
return 0;
}