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 i and j is at most k.

解题思路:

HashMap,JAVA实现如下:

    public boolean containsNearbyDuplicate(int[] nums, int k) {
HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;i++){
if(hm.containsKey(nums[i])&&i-hm.get(nums[i])<=k)
return true;
hm.put(nums[i], i);
}
return false;
}

Java for 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 [Leetcode 219]Contains Duplicate II

    题目描述: Given an array of integers and an integer k, find out whether 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. R语言画图实例-参考R语言实战

    dose <- c(, , , ,) drugA <- c(, , , , ) drugB <- c(, , , , ) # 数据准备 opar <- par(no.reado ...

  2. JQuery中的html(),text(),val()区别

    jQuery中.html()用为读取和修改元素的HTML标签,.text()用来读取或修改元素的纯文本内容,.val()用来读取或修改表单元素的value值. 1.HTML html():取得第一个匹 ...

  3. 第四天 rxcocoa

    HackerNewsReaderDemo HackerNewsAPI.sharedApi.newStories() .observeOn(ConcurrentDispatchQueueSchedule ...

  4. 制作自定义背景Button按钮、自定义形状Button的全攻略(转)

    在Android开发应用中,默认的Button是由系统渲染和管理大小的.而我们看到的成功的移动应用,都是有着酷炫的外观和使用体验的.因此,我们在开发产品的时候,需要对默认按钮进行美化.在本篇里,笔者结 ...

  5. CSS3-transform,2D动画实例

    对元素进行移动.缩放.转动.拉长 或 拉伸 全部都需要加前缀. Transform-2D转换方法:rotate()旋转.scale()缩放.skew()扭曲/倾斜.translate()位移.matr ...

  6. 贴一下WC总结里提到的那道裸题吧。。。

    [bzoj4034][HAOI2015]T2 试题描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 ...

  7. 解压版MySQL安装说明

    一.复制my.ini到MySQL解压的目录 例如:E:\MySQL 二.修改my.ini第39~40行 basedir = "E:\\MySQL" datadir = " ...

  8. word20161205

    cluster-aware application / 支持群集的应用程序 cluster-unaware application / 不支持群集的应用程序 Cluster.exe CNAME (ca ...

  9. MVC框架 与Smarty

    MVC一种软件设计模式 MVC全名是 Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据. ...

  10. Mac之vim普通命令使用[转]

    高级一些的编辑器,都会包含宏功能,vim当然不能缺少了,在vim中使用宏是非常方便的: :qx     开始记录宏,并将结果存入寄存器xq     退出记录模式@x     播放记录在x寄存器中的宏命 ...