题目描述:

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. 在一个字符串(1<=字符串长度<=10000,全部由大小写字母组成)中找到第一个只出现一次的字符,并返回它的位置

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  2. 【BZOJ】【3404】【USACO2009 Open】Cow Digit Game又见数字游戏

    博弈论 Orz ZYF 从前往后递推……反正最大才10^6,完全可以暴力预处理每个数的状态是必胜还是必败(反正才两个后继状态),然后O(1)查询……我是SB /******************** ...

  3. oracle——merge

      一.概述 使用merge声明从一个或者更多个表或视图中筛选记录,以用来更新或者插入到一个表或视图中.你可以指定条件以决定是执行update操作还是insert操作到目标表或视图中. 这个声明是一个 ...

  4. LA 4384

    扩展欧几里得 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  5. PHP杂记

    SOAP: 感觉是类似于Java中的HttpClient的东西,和curl也有点像. PHPStorm中查看所有的函数结构(Structure):Alt+7 查找方法或类(Symbol Name 函数 ...

  6. android2.3 -添加自定义按键:作唤醒功能 .

    最近需要做个唤醒功能,当按键的时候android系统唤醒并点亮屏,在长按键中,系统不能在进入睡眠. 驱动方面: 1:在平台设备文件中添加 一个按键,定义为唤醒源! \arch\arm\mach-s5p ...

  7. recursion lead to out of memory

    There are two storage areas involved: the stack and the heap. The stack is where the current state o ...

  8. 【hadoop2.6.0】倒排索引遇到问题了

    想实现书上倒排索引的例子,但是我不会java想用C++写,如果用hadoop streaming 那么输入必须是标准输入的形式, 那么我怎么获得每个文件的文件名呢? 查了一下,还有一种方法叫做hado ...

  9. POJ2151Check the difficulty of problems

    题意 : 举办一次比赛不容易,为了不让题目太难,举办方往往希望能够讲出的题目满足两点,1是所有的队伍都至少能够解出一个题目,2是冠军队至少能解出确定数量的题目,最后让你求的是每个队伍至少解出一道题并且 ...

  10. 2013 ACM-ICPC长沙赛区全国邀请赛——A So Easy!

    这题在比赛的时候不知道怎么做,后来看了别人的解题报告,才知道公式sn=(a+sqrt(b))^n+(a-sqrt(b))^n; 具体推导 #include<iostream> #inclu ...