-
Notifications
You must be signed in to change notification settings - Fork 0
/
missing_ranges.cpp
61 lines (56 loc) · 1.76 KB
/
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
/*
* =====================================================================================
*
* Filename: missing_ranges.cpp
*
* Description: 163. Missing Ranges. https://leetcode.com/problems/missing-ranges/
*
* Version: 1.0
* Created: 06/29/2024 18:19:53
* Revision: none
* Compiler: gcc
*
* Author: [email protected]
* Organization:
*
* =====================================================================================
*/
#include <tuple>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::tuple;
using std::vector;
class Solution {
public:
vector<vector<int>> findMissingRanges(vector<int>& nums, int lower, int upper) {
if (nums.size() == 0) {
return vector<vector<int>>{{lower, upper}};
}
vector<vector<int>> ranges;
if (nums.front() - lower) {
ranges.emplace_back(vector<int>{lower, nums.front() - 1});
}
for (int i = 1; i < nums.size(); i++) {
if (nums[i] - nums[i - 1] > 1) {
ranges.emplace_back(vector<int>{nums[i - 1] + 1, nums[i] - 1});
}
}
if (nums.back() < upper) {
ranges.emplace_back(vector<int>{nums.back() + 1, upper});
}
return ranges;
}
};
TEST(Solution, findMissingRanges) {
vector<tuple<vector<int>, int, int, vector<vector<int>>>> cases = {
std::make_tuple(vector<int>{}, 1, 1, vector<vector<int>>{{1, 1}}),
std::make_tuple(vector<int>{0, 1, 3, 50, 75}, 0, 99,
vector<vector<int>>{{2, 2}, {4, 49}, {51, 74}, {76, 99}}),
std::make_tuple(vector<int>{-1}, -1, -1, vector<vector<int>>{}),
};
for (auto& c : cases) {
EXPECT_EQ(Solution().findMissingRanges(std::get<0>(c), std::get<1>(c), std::get<2>(c)),
std::get<3>(c));
}
}