-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontains_duplicate2.cpp
More file actions
101 lines (94 loc) · 2.55 KB
/
contains_duplicate2.cpp
File metadata and controls
101 lines (94 loc) · 2.55 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* =====================================================================================
*
* Filename: contains_duplicate2.cpp
*
* Description: Contains Duplicate II. Given an array of integers and an integer k,
* find out whether there are two distinct indices i and j in the array
* such that nums[i] = nums[j] and the absolute difference between i
* and j is at most k.
*
* Version: 1.0
* Created: 02/26/19 09:03:17
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <unordered_map>
class Solution
{
public:
bool containsNearbyDuplicate(std::vector<int>& nums, int k)
{
#define __UNORDERED_MAP__
#ifdef __UNORDERED_MAP__
std::unordered_map<int, size_t> indexes;
for (size_t i = 0; i < nums.size(); i++)
{
auto iter = indexes.find(nums[i]);
if (iter != indexes.end())
{
if (i - iter->second <= k)
{
return true;
}
}
// Update index
indexes[nums[i]] = i;
}
return false;
#else
if (k < 1 || nums.size() < 2)
{
return false;
}
else if (k >= (nums.size() - 1))
{
// Same as 217 `Contains Duplicate`
std::sort(nums.begin(), nums.end());
for (size_t i = 1; i < nums.size(); i++)
{
if (nums[i] == nums[i - 1])
{
return true;
}
}
return false;
}
for (int i = 0; i < nums.size() - 1; i++)
{
for (int j = i + 1; ((j <= i + k) && (j < nums.size())); j++)
{
if (nums[i] == nums[j])
{
return true;
}
}
}
return false;
#endif
}
};
int main(int argc, char* argv[])
{
std::vector<int> nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 9};
if (argc > 2)
{
nums.clear();
for (int i = 1; i < argc; i++)
{
nums.push_back(atoi(argv[i]));
}
}
auto is_dup = Solution().containsNearbyDuplicate(nums, 3);
printf("Contains duplicate? %s\n", (is_dup ? "Yes" : "No"));
return 0;
}