题目描述:

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.

解题思路:

HashMap解决。

代码如下:

public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
int len = nums.length;
if(nums == null || len == 0)
return false;
for(int i = 0; i < len; i++){
if(!map.containsKey(nums[i]))
map.put(nums[i], i);
else {
if(i - map.get(nums[i]) <= k)
return true;
else
map.put(nums[i], i);
}
}
return false;
}
}

  

Java [Leetcode 219]Contains Duplicate II的更多相关文章

  1. [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 ...

  2. [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)

    每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...

  3. 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 ...

  4. 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 ...

  5. LeetCode 219 Contains Duplicate II

    Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...

  6. Leetcode 219 Contains Duplicate II STL

    找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...

  7. (easy)LeetCode 219.Contains Duplicate II

    Given an array of integers and an integer k, find out whether there there are two distinct indices i ...

  8. C#解leetcode 219. Contains Duplicate II

    该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...

  9. [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 ...

随机推荐

  1. 【BZOJ】【3275】Numbers

    网络流/最小割 Orz了Jiry_2神犇,蒟蒻网络流建模什么的完全不会啊T_T 按奇偶性来分组实在太巧妙了……然后相关的点之间连边表示只能选其一,来求最小割…… /****************** ...

  2. 如何将jsp中<input>设为只读

    将一个input变为只读,可以使用 readonly 属性 和 disabled 属性.  用disabled 属性时,文字显示为灰色.  下面的两种方法都是可以的: <input id =&q ...

  3. Linux GPT分区

    parted /dev/sdb  //parted  sdb磁盘 mklabel gpt        //设置该磁盘分区为gpt   mkpart primary 0% 100%   //划分分区大 ...

  4. BitNami一键安装Redmine(转)

    1. 简介 对于一个新手,如果严格按照官方文档来安装redmine,我想会“疯”掉的.有没有一种简便的方法.有滴,那就是BitNami. BitNami提供redmine的一键安装程序,简单.易用.方 ...

  5. linux出现bash: ./java: cannot execute binary file 问题的解决办法

    问题现象描述: 到orcal官网上下载了两个jdk: (1)jdk-7u9-linux-i586.tar.gz ------------>32位 (2)jdk-7u9-linux-x64.tar ...

  6. 使用wget和ftp共享文件

    一.需求 有一个机器A,上面那有很多文件.现在新买一个机器B,不想用U盘复制,就想把A弄成个服务器,然后B登录到A,想要什么文件就下载什么文件. 二.Win7实现 A是Win7和Ubuntu双系统,首 ...

  7. Sina App Engine(SAE)入门教程(8)- SaeFetchurl使用

    fetchurl是什么? FetchURL是SAE为开发者提供的分布式网页抓取服务,用来同步的抓取http页面,FetchURL针对国内的网络的做了优化,内部有调度系统,尽可能保证用户快速的抓取到目标 ...

  8. Photoshop:建议设置

    一.新建文档设置: 二.对齐设置 菜单->视图->对齐->全部 使用图层.形状等操作时自动对齐网格,画矢量图不怕模糊边缘,确保每个像素保持清晰. 三.首选项设置 关掉"启用 ...

  9. NDK 提示"undefined reference to xxx“的解决办法

    在Android.mk文件的 LOCAL_SRC_FILES后面加入包含该类或函数的文件,用\隔开,\后换行继续添加 例如 LOCAL_SRC_FILES := NDKTest.cpp\bncore. ...

  10. Windows JAVA 环境配置

    Java SE Development Kit Downloads http://www.oracle.com/technetwork/java/javase/overview/index.html ...