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 自定义实现 LRU 缓存算法
背景 LinkedHashMap继承自HashMap,内部提供了一个removeEldestEntry方法,该方法正是实现LRU策略的关键所在,且HashMap内部专门为LinkedHashMap提供 ...
- SQLServer 循环1百万插入测试数据
1,首先创建student表 create table student ( sno int primary key , sname VARCHAR(200) ) 2,--向数据库中插入100万条随机姓 ...
- ubuntu14.04 安装 StudioZend12
到官网下载:http://www.zend.com/en/products/studio/downloadsLinux-64位:http://downloads.zend.com/studio-ecl ...
- Chosen 基本使用
点击下载Chosen 引入文件 chosen.css jquery-1.7.1.min.js chosen.jquery.js 绑定数据: for (var i = 0; i < data.le ...
- python之路-模块安装 paramiko
paramiko介绍(全是洋文,看不懂啊,赶紧有道翻译吧,等有朝一日,我去报个华尔街): "Paramiko" is a combination of the esperanto ...
- Analyzing the Analyzers 分析分析师 —— 数据科学部门如何建
很多牛逼的公司都宣称在建立数据科学部门,这个部门该如何组建,大家都在摸石头过河. O‘reilly Strata今年 六月份发布了报告 <Analyzing the Analyzers>, ...
- [Hapi.js] Up and running
hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...
- Git 如何回到过去,然后 再 回到将来
回到过去: git log 然后 git reset --hard commit ID (那段长长代码 40位) 再,回到将来git reflog 然后 git reset --hard 前面那个代码 ...
- 测缘分程序c代码简单实现
#include<stdio.h>#include<stdlib.h>#include<math.h> #include <windows.h>//后面 ...
- javascript 之 this 用法
参考视频:http://www.imooc.com/video/6430 JavaScript中的this比较灵活,也是让很多初学者摸不到头脑,那么根据在不同的环境下,在同一个函数,不同的调用方式下, ...