当前位置:网站首页>力扣每日一题-第29天-219.存在重复元素Ⅱ
力扣每日一题-第29天-219.存在重复元素Ⅱ
2022-06-28 02:43:00 【重邮研究森】
2022.6.3今天你刷题了吗?
题目:
给你一个整数数组 nums 和一个整数 k ,判断数组中是否存在两个 不同的索引 i 和 j ,满足 nums[i] == nums[j] 且 abs(i - j) <= k 。如果存在,返回 true ;否则,返回 false 。
分析:
在一个数组里面,存在有一种重复元素,其个数>=2,我们要做的就是判断这个重复元素下标之差最小的是不是满足题目要求的k,存在下面特殊情况
0,1,2,0,0
这种情况,重复元素最小差为 1,也就是数组最后两个指下标做差
因此,结题思路就是最简单的暴力求解,我们依次遍历数组,求出每个重复元素之间的下标差,然后利用min函数求出最小,再进行比较就可以进行判断了。
解析:
1.暴力求解
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.哈希表
对于数组重复元素问题,哈希表是个好东西!!!
我们可以遍历一次数组,每次遍历时把数据按照(数值,下标)存入map,然后下次遍历的时候判断map里面是否已经存在该元素,如果存在并且下标条件满足则直接返回true,否则继续插入
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.滑动窗口
思想:由于给定k,那么我只需要判断k+1个数里面是否存在重复元素,如果存在,则必定返回true,如果不存在,则窗口移动,此时删除第一个元素,插入最后一个元素,再次判断是否存在重复的值。
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;
}
};
边栏推荐
- Notepad++--列编辑模式--用法/实例
- Ten years' experience of Software Engineer
- 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
- CURDATE()和NOW()区别
- Use js programming questions [split] in Niuke
- [games] Parkour
- Custom controls under WPF and adaption of controls in Grid
- 学习---有用的资源
- nn.Parameter和torch.nn.init系列函数给模型参数初始化
- xml&nbsp;文件的读写
猜你喜欢

第二轮红队免费公开课来袭~明晚八点!

In the digital era, enterprises must do well in user information security

17 `bs object Node name h3 Parent ` parents get parent node ancestor node

基于 LNMP 搭建个人网站的填坑之旅

调试利器 go-spew

可扩展存储系统(上)

数据库系列之MySQL配置F5负载均衡

matlab习题 —— 数据的基本处理

资源管理、高可用与自动化(下)

2022 electrician (elementary) recurrent training question bank and online simulation examination
随机推荐
Brief history and future trend of codeless software
Importer un fichier Excel, résoudre le problème de sauter les cellules vides et de ne pas lire, et avancer l'indice, et retourner Blank As NULL Red
失联修复:让“躲猫猫”无处可藏
Object class, and__ new__,__ init__,__ setattr__,__ dict__
导入Excel文件,解决跳过空白单元格不读取,并且下标前移的问题,以及RETURN_BLANK_AS_NULL报红
Inference optimization implementation of tensorrt model
ETCD数据库源码分析——集群间网络层服务端RaftHandler
爱普生L3153打印机如何清洗喷头
剑指 Offer 49. 丑数(三指针法)
音视频技术开发周刊 | 251
s32ds跳转到DefaultISR
2022 operation of simulated examination platform of special operation certificate examination question bank for safety management personnel of hazardous chemical business units
Introduction to kubernetes resource object and common commands
可扩展数据库(上)
建立自己的网站(17)
Apache - Introduction à Apache
No&nbsp;result&nbsp;defined&amp;nbsp…
Apache——阿帕奇簡介
kubernetes资源对象介绍及常用命令
Yes, it's about water