当前位置:网站首页>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;
}边栏推荐
- 单通道图片的读入
- 468. verifying the IP address
- (UE4 4.27) customize primitivecomponent
- Bert Chinese classification model training + reasoning + deployment
- Dlib face detection
- 勤于奋寻找联盟程序方法介绍
- Leetcode January 10 daily question 306 Additive number
- [word] word 2010 recording macro batch replacing paragraph marks in the selected text
- . Net core - pass Net core will Net to cross platform
- Video based fire smoke detection using robust AdaBoost
猜你喜欢

Book classification based on Naive Bayes

UE4 4.27 modify the mobile forward pipeline to support cluster multi light source culling

What states do threads have?

Directx11 advanced tutorial cluster based deffered shading

Nodemon cannot load the file c:\users\administrator\appdata\roaming\npm\nodemon PS1, because script execution is prohibited in this system

Poisson disk sampling for procedural placement

MNIST handwritten data recognition by RNN

Bert Chinese classification model training + reasoning + deployment

(UE4 4.27) UE4 adds a customized meshpass to realize the edge illumination of the mobile terminal

Tips for using the potplayer video player
随机推荐
Guns framework multi data source configuration without modifying the configuration file
The vs 2019 community version Microsoft account cannot be logged in and activated offline
Multithreading (4) -- no lock (3) -- longadder source code
单通道图片的读入
Redis queue
Idea common configuration
Project progress on February 28, 2022
Computer composition and design work06 —— 基于MIPS
N-degree Bessel curve
n次贝塞尔曲线
Unity implements smooth interpolation
Simulateur nightGod ADB View log
Storing texture2d to hard disk JPG file with script under unity3d
SQLite cross compile dynamic library
R language homework (IV): GDP value analysis of Shanghai and Tokyo from 1997 to 2018
Multithreading (2) -- pipeline (4) -- Park and unpark
Leetcode January 10 daily question 306 Additive number
Unity C script implements AES encryption and decryption
IBL of directx11 advanced tutorial PBR (3)
Unity3d multi platform method for reading text files in streamingasset directory