Contains Duplicate II 解答
Question
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.
Solution -- HashMap
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
int length = nums.length;
if (length <= 1 || k < 1)
return false;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int min = Integer.MAX_VALUE;
for (int i = 0; i < length; i++) {
if (map.containsKey(nums[i])) {
int prev = map.get(nums[i]);
int gap = i - prev;
min = Math.min(min, gap);
}
map.put(nums[i], i);
}
if (min <= k)
return true;
else
return false;
}
}
Contains Duplicate II 解答的更多相关文章
- [leetcode] Contains Duplicate II
Contains Duplicate II Given an array of integers and an integer k, find out whether there there are ...
- leetcode:Contains Duplicate和Contains Duplicate II
一.Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your fun ...
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- Contains Duplicate,Contains Duplicate II,Contains Duplicate III
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II
1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...
- 217/219. Contains Duplicate /Contains Duplicate II
原文题目: 217. Contains Duplicate 219. Contains Duplicate II 读题: 217只要找出是否有重复值, 219找出重复值,且要判断两者索引之差是否小于k ...
- [LeetCode] Contains Duplicate & Contains Duplicate II
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- 219. Contains Duplicate II【easy】
219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...
- [LeetCode] Contains Duplicate(II,III)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
随机推荐
- Java反编译插件jad
原文地址:http://www.cnblogs.com/JimLy-BUG/p/5405868.html 1.首先下载jar文件:net.sf.jadclipse_3.3.0.jar 下载 2. ...
- sql执行顺序整理
sql的执行顺序,是优化sql语句执行效率必须要掌握的.各个数据库可能有细小的差别,但大体顺序是相同的,这里只做大致说明. 一.总体执行顺序 在sql语句执行之前,还有SQL语句准备执行阶段,这里不做 ...
- VS2008下编译boost_1_47_0
1,boost下载 如果不想自己编译,可下载http://boostpro.com/download/boost_1_47_setup.exe,安装后,程序会提供选项供下载已经编译好的库文件,基于 ...
- TortoiseSVN和VisualSVN-下载地址
isualSVN的下载地址http://www.visualsvn.com/visualsvn/ 它可以以插件的形式嵌入到visual studio里面,让团队协作更轻松,最新的版本已经支持Visua ...
- 关于android的SQLiteDatabase和Cursor的一些疑问
android数据库操作的基础有三个类:SQLiteOpenHelper,SQLiteDatabase和Cursor.其中,SQLiteOpenHelper会建立一个数据库连接,它虽然可以调用多次ge ...
- ios 获取屏幕的属性
屏幕尺寸 CGRect screen = [UIscreen mainScreen].bounds 状态栏尺寸 CGRect rect = [[UIApplication sharedApp ...
- 关于char/varchar(n)中n的探究:字符数or字节数
[问题来源]将设计的数据库表展示的时候,yu哥问我,你的那个top_info字段定义的类型是varchar(100),为什么是100呢,这100的长度能存多少个中文? 当时的想法就是,这个100能存多 ...
- JDBC学习入门
一.JDBC相关概念介绍 1.1.数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡 ...
- Joomla 二次开发 学习笔记
Joomla目录结构 /administrator 管理后台的路径 /cache 是缓存目录 /components 是组件(component)目录 /includes 是一个重要的目录,里面都是J ...
- Spyder调试错误-"TypeError: decoding Unicode is not supported"
这是Spyder 2.7.4版本的一个Bug,升级到最新版本(2.7.9)即可. pip install --upgrade spyder Reference: https://github.com/ ...