-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path641.missing-ranges.cpp
62 lines (58 loc) · 1.49 KB
/
641.missing-ranges.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Tag: Simulation
// Time: O(N)
// Space: O(1)
// Ref: Leetcode-163
// Note: -
// Given a sorted integer array where **the range of elements are in the inclusive range [lower, upper]**, return its missing ranges.
//
// **Example 1**
// ```
// Input:
// nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99
// Output:
// ["2", "4->49", "51->74", "76->99"]
// Explanation:
// in range[0,99],the missing range includes:range[2,2],range[4,49],range[51,74] and range[76,99]
// ```
// **Example 2**
// ```
// Input:
// nums = [0, 1, 2, 3, 7], lower = 0 and upper = 7
// Output:
// ["4->6"]
// Explanation:
// in range[0,7],the missing range include range[4,6]
// ```
//
//
class Solution {
public:
/**
* @param nums: a sorted integer array
* @param lower: An integer
* @param upper: An integer
* @return: a list of its missing ranges
*/
vector<string> findMissingRanges(vector<int> &nums, int lower, int upper) {
// write your code here
vector<string> res;
long long low = lower;
for (int x: nums) {
if (low < x) {
res.push_back(output(low, x - 1));
}
low = x + 1LL;
}
if (low <= upper) {
res.push_back(output(low, upper));
}
return res;
}
string output(int low, int high) {
if (low == high) {
return to_string(low);
} else {
return to_string(low) + "->" + to_string(high);
}
}
};