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

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

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 }

 

转载地址:http://zmujl.baihongyu.com/

你可能感兴趣的文章
php 分页类
查看>>
Android中pm命令用法(转)
查看>>
Report_报表中Ref Cursor数据源的概念和用法(案例)
查看>>
python使用psutil获取服务器信息
查看>>
苹果新的编程语言 Swift 语言进阶(七)--枚举、结构、类
查看>>
常用Mysql存储引擎--InnoDB和MyISAM简单总结
查看>>
Spring Security笔记:登录尝试次数限制
查看>>
Atitit. 提升软件开发效率and 开发质量---java 实现dsl 4gl 的本质and 精髓 O725
查看>>
SQLMap用户手册【超详细】
查看>>
HDOJ 4884 & BestCoder#2 1002
查看>>
Validate Binary Search Tree leetcode java
查看>>
【编程题目】在二元树中找出和为某一值的所有路径(树)
查看>>
脱裤子放屁,多此一举
查看>>
445port入侵具体解释
查看>>
Git学习笔记(二)
查看>>
美妙的 CSS3 动画!一组梦幻般的按钮效果
查看>>
微软历史最高市值是多少?
查看>>
Linux Shell脚本Ldd命令原理及使用方法
查看>>
[ucgui] 对话框8——Framewin小工具
查看>>
Ununtu 12.04 gedit安装插件Source Code Browser
查看>>