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. 淘宝首页源码藏美女彩蛋(下)(UED新作2013egg)

    我们已经知道,执行美女会得到"彩蛋",而正是彩蛋做到了taobaoUED展现给大家的神奇的前端魅力.今天我们来看看FP.egg&&FP.egg("%cjo ...

  2. [译]git rebase -i

    使用rebase -i会在终端出现一个交互页面. 在这个交互页面中我们可以对要rebase的commit做一定的修改. 用法 git rebase -i <master> 把当前的分支的c ...

  3. Codeforces Round #270 1002

    Codeforces Round #270 1002 B. Design Tutorial: Learn from Life time limit per test 1 second memory l ...

  4. 大数据之pig 命令

    1.pig与hive的区别 pig和hive比较类似的,都是类sql的语言,底层都是依赖于hadoop    走的mapreduce任务.    pig和hive的区别就是,想要实现一个业务逻辑的话, ...

  5. 如何解决Mac里面解压后文件名乱码问题

    如果你把Mac当成你的主要工作机器,而你的同事用的都是Windows,有时候交换文档就是一件很痛苦的事,比如今天要说到的问题:当同事传给你一个zip文件,结果你拿过来解压后发现里面有些文件的文件名如果 ...

  6. Ubuntu 12 编译安装 PHP 5.4 及 问题汇总

    参考先前的文章:Ubuntu 14 编译安装 PHP 5.4.45 + Nginx 1.4.7 + MySQL 5.6.26 笔记 安装过程: ############################ ...

  7. JAVA-Excel文件操作

    使用环境:JAVA 1.8 一.安装 1.下载Poi包 Apache POI 当前最新稳定版本为3.14.下载poi-bin-3.14.zip即可. 2.将下载下来的压缩包解压,将其中的所有jar文件 ...

  8. php 通过curl post发送json数据实例

    例1  代码如下 复制代码 $data = array("name" => "Hagrid", "age" => "3 ...

  9. struts2-(2)HelloWorld

    1.环境配置 1).进入http://struts.apache.org/download.cgi#struts23241 下载 struts官方源码 2).解压,进入apps/struts2-bla ...

  10. HDU 1174 爆头(计算几何)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1174 解题报告:就是用到了三维向量的点积来求点到直线的距离,向量(x1,y1,z1)与(x2,y2,z ...