-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path219.cpp
41 lines (38 loc) · 925 Bytes
/
219.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
#include <gtest/gtest.h>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
bool containsNearbyDuplicate(vector<int> &nums, int k)
{
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
if (map.find(nums[i]) != map.end() && i - map[nums[i]] <= k) {
return true;
}
map[nums[i]] = i;
}
return false;
}
};
class TestSolution : public ::testing::Test {
protected:
Solution sol;
};
TEST_F(TestSolution, t1)
{
vector<int> nums = {1, 2, 3, 1};
int k = 3;
bool output = sol.containsNearbyDuplicate(nums, k);
bool expected = true;
ASSERT_EQ(output, expected);
}
TEST_F(TestSolution, t2)
{
vector<int> nums = {1, 0, 1, 1};
int k = 1;
bool output = sol.containsNearbyDuplicate(nums, k);
bool expected = true;
ASSERT_EQ(output, expected);
}