-
Notifications
You must be signed in to change notification settings - Fork 2
/
163.cpp
32 lines (28 loc) · 1.01 KB
/
163.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
/*Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], return its missing ranges.
For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "76->99"].*/
class Solution {
public:
vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
vector<string> res;
int cur = lower;
bool flow = false;
for (int num : nums) {
if (num < cur) continue;
else if (num > cur) {
res.push_back(generate(cur, num-1));
}
if (num > 0 && num+1 < 0) {
flow = true;
cur = num;
} else {
cur = num+1;
}
}
if (!flow && upper >= cur) res.push_back(generate(cur, upper));
return res;
}
string generate(int eval, int rval) {
if (rval == eval) return to_string(eval);
else return to_string(eval)+"->"+to_string(rval);
}
};