当前位置:网站首页>Li Kou daily question - day 29 -219 Duplicate Element II exists
Li Kou daily question - day 29 -219 Duplicate Element II exists
2022-06-28 03:37:00 【Chongyou research Sen】
2022.6.3 Did you brush the questions today ?
subject :
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 .
analysis :
In an array , There is a repeating element , The number of them >=2, What we need to do is to judge whether the lowest subscript difference of this repeating element meets the requirements of the topic k, There are the following special cases
0,1,2,0,0
This situation , The minimum difference of repeated elements is 1, That is, the difference between the last two subscripts of the array
therefore , The conclusion idea is the simplest violent solution , We iterate through the array in turn , Find the subscript difference between each repeating element , And then use it min Function to find the minimum , Then we can make a comparison and make a judgment .
analysis :
1. Violent solution
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
int inf = 1e9;
int res = inf;
for (auto i=0;i<nums.size()-1;++i)
{
for (auto j = i + 1; j < nums.size(); ++j)
{
if (nums[i] == nums[j])
{
res = min(res, j - i);
}
}
}
if (res <= k)
{
return true;
}
else
{
return false;
}
return 0;
}
};
2. Hashtable
For the array repeating element problem , Hash tables are a good thing !!!
We can traverse the array once , Each time, the data is traversed according to ( The number , Subscript ) Deposit in map, Then the next time you traverse it, you can judge map Whether the element already exists in it , If it exists and the subscript condition is met, it directly returns true, Otherwise, continue to insert
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int, int> map;
for (auto i=0;i<nums.size();i++)
{
int num = nums[i];
if (map.count(num) && i - map[num] <= k)
{
return true;
}
map[num] = i;
}
return false;
}
};
3. The sliding window
thought : Given k, Then I just need to judge k+1 Whether there are duplicate elements in the number , If there is , It must return true, If it doesn't exist , Then the window moves , The first element is deleted , Insert the last element , Again, determine whether there are duplicate values .
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_set<int>set;
for (auto i = 0; i < nums.size(); ++i)
{
if (i > k)
{
set.erase(nums[i - k - 1]);
}
if (set.count(nums[i]))
{
return true;
}
set.emplace(nums[i]);
}
return false;
}
};
边栏推荐
- Is it safe to buy stocks and open an account through the account opening link of the broker manager? Want to open an account for stock trading
- 力扣每日一题-第29天-523.在区间范围统计奇数数目
- 可扩展数据库(下)
- 文档问题
- Etcd database source code analysis -- network layer server rafthandler between clusters
- 栈的基本操作(C语言实现)
- 2022 safety officer-c certificate examination question bank simulated examination platform operation
- crond BAD FILE MODE /etc/cron.d
- Resource management, high availability and automation (Part 2)
- 在牛客中使用JS编程题【split】
猜你喜欢
栈的基本操作(C语言实现)
剑指 Offer 47. 礼物的最大价值(DP)
Question bank and answers of special operation certificate for R1 quick opening pressure vessel operation in 2022
matlab习题 —— 符号运算相关练习
【小程序】使用font-awesome字体图标的解决文案(图文)
2022电工(初级)复训题库及在线模拟考试
用于 C# 的 SQL 基本语法总结
導入Excel文件,解决跳過空白單元格不讀取,並且下標前移的問題,以及RETURN_BLANK_AS_NULL報紅
开口式霍尔电流传感器如何助力直流配电改造?
Set drop-down options on Excel files
随机推荐
资源管理、高可用与自动化(中)
nn. Parameter and torch nn. Init series of functions to initialize model parameters
Ten years' experience of Software Engineer
Hot! Yolov6's fast and accurate target detection framework is open source (with source code download)
matlab习题 —— 矩阵的常规运算
Is Guotai Junan Securities reliable? Is it safe to open a securities account?
基于 LNMP 搭建个人网站的填坑之旅
Anaconda命令用法
如何给Eclips自动添加作者,时间等…
栈的基本操作(C语言实现)
基于 WPF 的酷炫 GUI 窗口的简易实现
数据库系列之MySQL和TiDB中慢日志分析
文件的相对路径写法
Is it better for a novice to open a securities account? Is it safe to open a stock trading account
ETCD数据库源码分析——集群间网络层服务端RaftHandler
Brief history and future trend of codeless software
2022 electrician (elementary) recurrent training question bank and online simulation examination
劲爆!YOLOv6又快又准的目标检测框架开源啦(附源代码下载)
Summary of the use of composition API in the project
How to write concise code? (upper)