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. Linux 一条长命令占用多行

    前言 考察下面的脚本: ? 1 emcc -o ./dist/test.html --shell-file ./tmp.html --source-map-base dist -O3 -g4 --so ...

  2. 使用jquery修改display属性

    var show = $('#test').css('display'); $('#test').css('display',show =='block'?'none':show); 这段代码通过判断 ...

  3. js遍历删除数组中不符合条件的元素

    //一般解决方法 let arr = [1,2,3]; for(let i=0; i<arr.length; i++){ if(arr[i]==2){ arr.splice(i, 1); i-- ...

  4. POJ2421Constructing Roads

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23343   Accepted: 10 ...

  5. golang 斐波那契数

    golang 斐波那契数 package main import "fmt" /* 斐波那契数,亦称之为斐波那契数列(意大利语: Successione di Fibonacci) ...

  6. 利用斗图啦网站API批量下载表情图片

    decorator.py #!/usr/bin/env python # -*- coding: utf-8 -*- import logging import os from functools i ...

  7. Intellij IDEA中maven项目打包问题

    学习使用java写项目的时候,java的jar包对我来说是很神奇又很复杂不想去了解的东西,如今形势所迫开始写java项目,做了些了解,也有几个问题. 1.其中一个打包方式 在pom文件中输入如下插件( ...

  8. 【caffe Layer】代码中文注释

    src/caffe/proto/caffe.proto 中LayerParameter部分 // NOTE // Update the next available ID when you add a ...

  9. EasyTrader踩坑之旅(三)

    快速阅读 ​ 用THSTrader 调试同花顺自动下单的过程 . 主要原理是利用python函数pywinauto 自动获取同花顺上相应控件的值,进行模拟自动化的操作,不得不说python函数库的强大 ...

  10. html5中hgroup和address标签使用总结

    html5中hgroup和address标签使用总结 一.总结 一句话总结: hgroup元素(不推荐使用):用来给标题分组,通常放在header中: address元素:斜体显示:用来说明作者的联系 ...