219. Contains Duplicate II

Easy

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

Example 1:

Input: nums = [1,2,3,1], k = 3
Output: true

Example 2:

Input: nums = [1,0,1,1], k = 1
Output: true

Example 3:

Input: nums = [1,2,3,1,2,3], k = 2
Output: false
package leetcode.easy;

public class ContainsDuplicateII {
public boolean containsNearbyDuplicate(int[] nums, int k) {
java.util.HashMap<Integer, Integer> map = new java.util.HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i]) && (i - map.get(nums[i]) <= k)) {
return true;
} else {
map.put(nums[i], i);
}
}
return false;
} @org.junit.Test
public void test() {
int[] nums1 = { 1, 2, 3, 1 };
int[] nums2 = { 1, 0, 1, 1 };
int[] nums3 = { 1, 2, 3, 1, 2, 3 };
int k1 = 3;
int k2 = 1;
int k3 = 2;
System.out.println(containsNearbyDuplicate(nums1, k1));
System.out.println(containsNearbyDuplicate(nums2, k2));
System.out.println(containsNearbyDuplicate(nums3, k3));
}
}

LeetCode_219. Contains Duplicate II的更多相关文章

  1. [leetcode] Contains Duplicate II

    Contains Duplicate II Given an array of integers and an integer k, find out whether there there are ...

  2. leetcode:Contains Duplicate和Contains Duplicate II

    一.Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your fun ...

  3. 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II

     217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...

  4. Contains Duplicate,Contains Duplicate II,Contains Duplicate III

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  5. LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II

     1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...

  6. 217/219. Contains Duplicate /Contains Duplicate II

    原文题目: 217. Contains Duplicate 219. Contains Duplicate II 读题: 217只要找出是否有重复值, 219找出重复值,且要判断两者索引之差是否小于k ...

  7. [LeetCode] Contains Duplicate & Contains Duplicate II

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

  8. 219. Contains Duplicate II【easy】

    219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...

  9. [LeetCode] Contains Duplicate(II,III)

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

随机推荐

  1. shellshock溢出攻击

    实验背景 2014年9月24日,Bash中发现了一个严重漏洞shellshock,该漏洞可用于许多系统,并且既可以远程也可以在本地触发.在本实验中,需要亲手重现攻击来理解该漏洞,并回答一些问题. 什么 ...

  2. Attention Model详解

    要是关注深度学习在自然语言处理方面的研究进展,我相信你一定听说过Attention Model(后文有时会简称AM模型)这个词.AM模型应该说是过去一年来NLP领域中的重要进展之一,在很多场景被证明有 ...

  3. java spring boot 导出/下载文本文件操作(包含写文本文件)

    内容简介 本文主要内容为使用java把内容写入文本文件,并实现下载/导出的功能. 实现步骤 1. controller层 @ResponseBody @RequestMapping(value = & ...

  4. python3文本读取与写入常用代码

    创建文件夹: import os import shutil def buildfile(echkeyfile): if os.path.exists(echkeyfile): #创建前先判断是否存在 ...

  5. Oracle row_number() over() 分析函数--取出最新数据

    语法格式:row_number() over(partition by 分组列 order by 排序列 desc) 一个很简单的例子 1,先做好准备 create table test1( id v ...

  6. Phoenix 简单介绍

    转载自:https://blog.csdn.net/carolzhang8406/article/details/79455684 1. Phoenix定义 Phoenix最早是saleforce的一 ...

  7. C void的指针 强制类型转换(int*)p 把变量指针p强制转换成指向int类型的指针

    #include <stdio.h> int main(void){ void *p; int a = 14322; char c ='A'; p = &a; //p = & ...

  8. noi.ac #46 最长上升子序列

    \(des\) 长度为 \(n\) 的序列 \(A\),从中删去恰好 \(k\) 个元素(右边的元素往左边移动),记 \(cnt\) 为新 序列中 \(Ai = i\) 的元素个数(即权值与下标相同的 ...

  9. #6085. 「美团 CodeM 资格赛」优惠券

    题目描述 用last[x]表示对x进行的上一次操作的位置,vis[x]表示x是否在大楼内. Splay维护'?'的位置. 若x要进楼: 1.若x已在楼内,则去找last[x]到i之间是否有'?',若有 ...

  10. [C++]线程池 与 [Go] mapreduce

    线程池 ref: https://github.com/progschj/ThreadPool/blob/master/ThreadPool.h ref: https://www.jianshu.co ...