aboutsummaryrefslogtreecommitdiff
path: root/contains_duplicate2.cpp
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /contains_duplicate2.cpp
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'contains_duplicate2.cpp')
-rw-r--r--contains_duplicate2.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/contains_duplicate2.cpp b/contains_duplicate2.cpp
new file mode 100644
index 0000000..9af7758
--- /dev/null
+++ b/contains_duplicate2.cpp
@@ -0,0 +1,24 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ bool containsNearbyDuplicate(vector<int> &nums, int k) {
+ unordered_map<int, int> um;
+ for (int i = 0; i < nums.size(); i++) {
+ if (um.count(nums[i]))
+ if (abs(i - um[nums[i]]) <= k)
+ return true;
+ um[nums[i]] = i;
+ }
+ return false;
+ }
+};
+
+int main() {
+ Solution obj;
+ vector<int> nums = {1, 2, 3, 1};
+ if (obj.containsNearbyDuplicate(nums, 3))
+ cout << "true";
+ else
+ cout << "false";
+}