当前位置:网站首页>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;
}边栏推荐
- Simple spiral ladder generation for Houdini program modeling
- English语法_副词_有无ly,意义不同
- 线程有哪些状态?
- Leetcode-139. Word splitting
- LeetCode-剑指Offer(第二版)个人题解完整版
- Piecewise Bezier curve
- Redis queue
- (UE4 4.27) customize globalshader
- How do I get the date and time from the Internet- How to get DateTime from the internet?
- Jetson TX2 machine brushing jetpack4.2 (self test successful version)
猜你喜欢

(UE4 4.27) add globalshder to the plug-in

Bert use

Leetcode January 10 daily question 306 Additive number

Opencv_ 100 questions_ Chapter V (21-25)

AI operation ch8

2D human pose estimation for pose estimation - pifpaf:composite fields for human pose estimation

BRDF of directx11 advanced tutorial PBR (2)

Qt-- realize TCP communication

Why doesn't the database use binary tree, red black tree, B tree and hash table? Instead, a b+ tree is used

Directx11 advanced tutorial PBR (1) summary of physical phenomena of light
随机推荐
Redis queue
Getting started with houdininengine HDA and UE4
468. verifying the IP address
AI作业ch8
Multithreading (2) -- pipeline (4) -- Park and unpark
Multithreading (4) -- no lock (2) -- Atomic related atomic classes
Directx11 advanced tutorial PBR (1) summary of physical phenomena of light
RNN model
Multithreading mode (I) -- protective pause and join source code
Computer composition and design work05 ——fifth verson
Leetcode January 10 daily question 306 Additive number
Cv2.fillpoly coco annotator segment coordinate conversion to mask image
(UE4 4.27) customize primitivecomponent
获取图片的尺寸
Video fire detection based on Gaussian mixture model and multi-color
(UE4 4.27) UE4 adds a customized meshpass to realize the edge illumination of the mobile terminal
夜神模拟器adb查看log
Leetcode personal question solution (Sword finger offer3-5) 3 Duplicate number in array, 4 Find in 2D array, 5 Replace spaces
Video summary with long short term memory
Nodemon cannot load the file c:\users\administrator\appdata\roaming\npm\nodemon PS1, because script execution is prohibited in this system