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 ...
随机推荐
- 处理ASP.NET 请求(IIS)
原文:http://www.cnblogs.com/hkncd/archive/2012/03/23/2413917.html 英文原文:Beginner’s Guide: How IIS Proce ...
- JAVA学习笔记--二
一.抽象类: 访问修饰符 abstract class 类名{ } 抽象类和普通类的区别: 1. 抽象类不能被实例化 2. 抽象类一般含有抽象方法 抽象方法:在抽象类中只有方法签名(方法声明),没有方 ...
- javascript权威指南第6版学习笔记
javascript权威指南第6版学习笔记 javascript数组.函数是特殊对象 看一点少一点. 3.1.4 hello.js内容是 var x=.3-.2;var y=.2-.1 console ...
- SQL存储过程基于字段名传入的字符串拼接.
--定义存储过程. Create PROCEDURE Usp_Static ), ), --分组字段. ), --统计字段. ), --表头字段. ) --聚会函数. AS ) --存储游标执行的列. ...
- PHP代码批量加密
<?php error_reporting(E_ALL); ini_set('display_errors','1'); //批量加密码当前目录 $dirnow = getcwd(); $dir ...
- 【原创】Git版本控制器的基本使用
关于git Git,是一个分布式版本控制软件.最初本是为了更好的管理Linux内核开发而被林纳斯·托瓦兹开发,后来因为项目开发中版本控制的强烈需求,而git也日趋成熟,最终成为了一个独立的版本控制软件 ...
- iOS中JavaScript和OC交互
转载自:http://www.devzeng.com/blog/ios-uiwebview-interaction-with-javascript.html 还可参考的文章:http://blog.c ...
- Linux通配符
* 任意字符 ?任意单个字符 [] 匹配指定 字符范围内的字符 [^] 指定范围之外的单个字符 常规字符集合 [a-z] a到z的所有小写字母 [A-Z] a到z的所有大写字母 [0-9] 0到9的所 ...
- WebService开发步骤
WebService原理什么的百度很多我就说了,无非就是提供一个接口放在服务端,客户端实现这个接口就可以拿到自己需要的东西. 现在简单说一下用myEclipse来实现服务端和客户端.另一种WebSer ...
- servlet的提交
servlet的提交和动态改变有点依赖xml 我们点击控件的时候改变了里面的变量,改变了xml,servlet发现变量变了,就会刷新页面 如果xml文档没有更新,浏览器采用缓存而不则行 <for ...