219. Contains Duplicate II - LeetCode
Question
Solution
题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false
思路:用一个map记录每个数的坐标,如果数相同,如果坐标差小于k则返回true否则覆盖,继续循环
Java实现:
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int tmp = nums[i];
if (map.get(tmp) != null) {
// System.out.println(i + "---" + map.get(tmp));
if (i - map.get(tmp) <= k) return true;
}
map.put(tmp, i);
}
return false;
}
219. Contains Duplicate II - LeetCode的更多相关文章
- 219. Contains Duplicate II【easy】
219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- LeetCode 219. Contains Duplicate II (包含重复项之二)
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- 【一天一道LeetCode】#219. Contains Duplicate II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】219. Contains Duplicate II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用set 使用字典 日期 题目地址:https:/ ...
- LeetCode 219 Contains Duplicate II
Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...
- Java for LeetCode 219 Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i ...
- Leetcode 219 Contains Duplicate II STL
找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...
随机推荐
- buuctf 荷兰带宽数据泄露
荷兰带宽数据泄露 下载附件得一个conf.bin文件,这个文件是路由信息文件,题目并没有任何提示,我们先来测试一下最简单的,找username或password然后当作flag交上去,我们使用Rout ...
- -> 在c语言中是什么意思?
->在C语言中称为间接引用运算符,是二目运算符,优先级同成员运算符".".用法:p->a,其中p是指向一个结构体的指针,a是这个结构体类型的一个成员.表达式p-> ...
- char向wchar的转换-MultiByteToWideChar
问题产生 使用CreateFile函数,如下: CreateFile(lpcTheFile, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NO ...
- 从零开始开发一款H5小游戏(三) 攻守阵营,赋予粒子新的生命
本系列文章对应游戏代码已开源 Sinuous game. 每个游戏都会包含场景和角色.要实现一个游戏角色,就要清楚角色在场景中的位置,以及它的运动规律,并能通过数学表达式表现出来. 场景坐标 canv ...
- pydev+eclipse写python代码
首先,下载pydev:PyDev for Eclipse - Browse /pydev at SourceForge.net (建议下载到本地,之前看其他文章时,进行了如下安装: 启动 Eclips ...
- npm权限不够(安装什么都报错)
问题 Windows下使用npm安装任何包都报错, Windows下使用npm显示权限不够 如图: 解决方法 1. 方法一 使用管理员权限打开 命令窗口, 治标不治本!!!!不推荐 ...
- 居中select中的option选项
select经常有长短不一的选项:选择不同的选项居中不会生效: 使用text-align:center;text-align-last: center; 可以让所有选项都居中: 关于text-dec ...
- 将对象push到数组中组成对象数组
let items = { key:'', value:'' } for(let i = 0;i<len;i++){ items.value = _this.ills[i].sName; ite ...
- Ncrystal Skill设计
在使用allegro时一般都会听说过skill,使用合适的Skill会使事情事半功倍.但是现阶段所能看到的个人白嫖的Skill都有一些通病.所以我才开发符合自己操作习惯的Skill. 当前我们所能找的 ...
- .net 使用Docker开发
.NET多年以前已经开始支持Docker,但由于国内.net现状,生产过程中几乎用不到docker支持,趁着有点时间捣鼓下~. 先期工作 1.首先安装 Docker Desktop 2.安装Visua ...