Contains Duplicate II ——LeetCode
Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k.
题目大意:给定一个数组,和一个整数k,找出是否有两个不同的下标使得nums[i] = nums[j] ,并且i和j相距不超过k。
解题思路:使用两个map,保存一个数的两个下标,一大一小,并在遍历数组时更新它。
public boolean containsNearbyDuplicate(int[] nums, int k) {
if(nums==null||nums.length==0){
return false;
}
Map<Integer,Integer> minMap = new HashMap<>();
Map<Integer,Integer> maxMap = new HashMap<>();
for(int i=0;i<nums.length;i++){
if(minMap.get(nums[i])!=null){
int min=minMap.get(nums[i]);
if(maxMap.get(nums[i])!=null){
int max=maxMap.get(nums[i]);
if((min!=max&&max-min<=k)||i-max<=k){
return true;
}
minMap.put(nums[i],max);
maxMap.put(nums[i],i);
continue;
}
}
minMap.put(nums[i],i);
maxMap.put(nums[i],i);
}
return false;
}
Contains Duplicate II ——LeetCode的更多相关文章
- 219. Contains Duplicate II - LeetCode
Question 219. Contains Duplicate II Solution 题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false 思路:用一个map记录每 ...
- Contains Duplicate II leetcode
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- [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 ...
- LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II
1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...
- [LeetCode] Contains Duplicate & Contains Duplicate II
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- [LeetCode] Contains Duplicate(II,III)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
随机推荐
- Stream流的读取使用
这个是在现在的项目里,用到的知识点,一般用不到这些..所以想着还是记下来以后用. 针对文本流 //StreamReader sr = new StreamReader(mystream,Encodin ...
- js substr和substring字符串截取
substr(start,length)第一个参数是开始位置(注:start的开始是从0开始,看到好多博客上面是从1开始,在火狐和谷歌执行了一下是从0开始),第二个参数是截取字符串的长度(可以省略,表 ...
- 数据库导出导入操作(expdp,impdp)
EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用. 命令行: sqlplus/nolog connect username/password as sysd ...
- C++ Reference 的“三位一体”诠释
C++ 是介于汇编语言与高级语言之间的一种“全能”语言.它的能力是其他任何基于VMA(冯-诺曼架构)计算机的高级程序设计语言无法望其项背的,而性能也只有C语言可与之伯仲. 然而长期以来,喜欢C++和憎 ...
- 《用chsh选择shell》-linux命令五分钟系列之十二
chsh命令用于修改你的登录shell. 1 我想知道我机器安装了哪些shell? 两种方法可以查看: 第一种: [rocrocket@wupengchong ~]$ chsh -l /bin/sh ...
- ie8 background css没有显示?——都是空格惹的祸
ie8 background css没有显示?——都是空格惹的祸
- 入门1:PHP的优点
一.语法简单 二.学习成本低 (因为语法简单 所以学习成本低) 三.开发效率高 (PHP运行流程很简单,语法也很简单,必然开发效率就高) 四.跨平台 (我们只需要写一份PHP的程序,就可以非常方便的把 ...
- myisam和innodb区别
InnoDB MyIsam 事务 支持 不支持 锁 行锁 表锁 索引 B+树,数据和索引在一个文件中,必须有主键,如果不指定,会自动生成一个隐藏字段作 ...
- mac 自带 php 验证码 不显示
curl -s http://php-osx.liip.ch/install.sh | bash -s 5.5 在命令行中输入上面 ,最后5.5是php版本号 自动安装FreeType ----- ...
- 使用$.getJSON实现跨域ajax请求
jQuery中常用getJSON来调用并获取远程的JSON字符串,将其转换为JSON对象,如果成功,则执行回调函数.原型如下: jQuery.getJSON( url, [data], [callba ...