博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Contains Duplicate II
阅读量:7075 次
发布时间:2019-06-28

本文共 925 字,大约阅读时间需要 3 分钟。

Well, the basic idea is fairly straightforward. We maintain a mapping mp from a value in nums to its position (index) i. Each time we meet an unseen value, we add it to the map (mp[nums[i]] = i). Otherwise, depending on whether the recorded index mp[nums[i]] and the current index isatisfy i - mp[nums[i]] <= k (node that the new index i is larger than the old indexmp[nums[i]]), we return true or update the index (mp[nums[i]] = i). If all the elements have been visited and we have not returned true, we will return false.

1     bool containsNearbyDuplicate(vector
& nums, int k) {2 unordered_map
mp; 3 for (int i = 0; i < nums.size(); i++) {4 if (mp.find(nums[i]) != mp.end() && i - mp[nums[i]] <= k)5 return true;6 mp[nums[i]] = i; 7 }8 return false; 9 }

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4548006.html

你可能感兴趣的文章
环视 Lookaround ---- 正则
查看>>
MySQL事务
查看>>
定义一个空的C++类,编译器自动创建四类成员函数
查看>>
SEO基本概念入门
查看>>
代码实现 UITableView与UITableViewCell
查看>>
用python写的agent
查看>>
微信天气接口查询
查看>>
spring-前置通知
查看>>
Transient修饰符的使用
查看>>
shell特殊符号,cut、sort、wc、uniq、tee、tr、split命令
查看>>
运维面试题
查看>>
java 消息摘要算法 MAC
查看>>
2011.11.6
查看>>
Linux系统获取命令帮助方法及简单命令介绍
查看>>
PyYAML序列化yaml文件数据
查看>>
Radmin远程连接TMG
查看>>
CCNA 学习笔记(三)--路由选择协议(静态路由协议)
查看>>
python 学习笔记(4)-转载
查看>>
python实例pyspark以及python中文显示
查看>>
一个典型核心网络故障分析
查看>>