当前位置:网站首页>LeetCode-219. Duplicate Element II present
LeetCode-219. Duplicate Element II present
2022-06-12 06:23:00 【Border wanderer】
Give you an array of integers nums And an integer k , Determine whether there are two... In the array Different indexes i and j , Satisfy nums[i] == nums[j] And abs(i - j) <= k . If there is , return true ; otherwise , return false .
Example 1:
Input :nums = [1,2,3,1], k = 3
Output :true
Example 2:
Input :nums = [1,0,1,1], k = 1
Output :true
Example 3:
Input :nums = [1,2,3,1,2,3], k = 2
Output :false
Tips :
1 <= nums.length <= 105
-109 <= nums[i] <= 109
0 <= k <= 105
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_set<int> mp;
unordered_set<int>::iterator first;
/* Input verification */
if (nums.empty() || nums.size() == 1) {
return false;
}
/* A special case ,k Greater than the length of the array , Just judge once */
if (k >= nums.size()) {
for (int i = 0; i < nums.size(); i++) {
if (mp.count(nums[i]) == 0) {
mp.insert(nums[i]);
}
else {
return true;
}
}
return false;
}
/* k Less than array length */
else {
for (int i = 0; i <= k; i++) {
if (mp.count(nums[i]) == 0) {
mp.insert(nums[i]);
}
else {
return true;
}
}
}
/* Building sliding windows */
for (int i = k + 1; i < nums.size(); i++) {
first = mp.find(nums[i - k - 1]);
mp.erase(first);
if (mp.count(nums[i]) == 0) {
mp.insert(nums[i]);
}
else {
return true;
}
}
return false;
}
};
int main() {
vector<int> nums = { 1, 2, 3,1,2, 3 };
Solution* ps = new Solution();
cout << ps->containsNearbyDuplicate(nums, 2) << endl;
system("pause");
return 0;
}边栏推荐
- C2w model - language model
- How do I get the date and time from the Internet- How to get DateTime from the internet?
- Univariate linear regression model
- Leetcode-1535. Find the winner of the array game
- Jetson TX2 machine brushing jetpack4.2 (self test successful version)
- On the normalization of camera rotation interpolation
- Script for unity3d to recursively search for a node with a specific name from all child nodes of a node
- N-degree Bessel curve
- SQLite cross compile dynamic library
- About why GPU early-z reduces overdraw
猜你喜欢

Bulk Rename Utility

Jetson TX2 machine brushing jetpack4.2 (self test successful version)

MNIST handwritten data recognition by CNN

Multithreading (V) -- concurrency tools (I) -- thread pool (II) -- related contents of ThreadPoolExecutor

Directx11 advanced tutorial PBR (1) summary of physical phenomena of light

Simulateur nightGod ADB View log

相机图像质量概述

Three years of sharpening a sword: insight into the R & D efficiency of ant financial services

Logistic regression model

Using hidden Markov model to mark part of speech
随机推荐
Summary of some problems in sensor bring up
Redis data structure (VIII) -- Geo
Multithreading (2) -- pipeline (4) -- Park and unpark
关于 Sensor flicker/banding现象的解释
SQLite cross compile dynamic library
RNN implementation regression model
Redis configuration (IV) -- cluster
Leetcode-139. Word splitting
Project progress on February 28, 2022
Multithreading (4) -- no lock (2) -- Atomic related atomic classes
Touch screen setting for win7 system dual screen extended display
On the normalization of camera rotation interpolation
Computer composition and design work06 —— 基于MIPS
Remap function of C different interval mapping
GET 和 POST 的区别及留言板代码实现
Open the camera in unity3d and display the contents of the camera in the scene as texture2d
Chartextcnn (Ag dataset - news topic classification)
OverFeat: Integrated Recognition, Localization and Detection using Convolutional Networks
First note
Redis queue