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:

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

Example 2:

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

Example 3:

  1. Input: nums = [1,2,3,1,2,3], k = 2
  2. Output: false
  1. package leetcode.easy;
  2.  
  3. public class ContainsDuplicateII {
  4. public boolean containsNearbyDuplicate(int[] nums, int k) {
  5. java.util.HashMap<Integer, Integer> map = new java.util.HashMap<Integer, Integer>();
  6. for (int i = 0; i < nums.length; i++) {
  7. if (map.containsKey(nums[i]) && (i - map.get(nums[i]) <= k)) {
  8. return true;
  9. } else {
  10. map.put(nums[i], i);
  11. }
  12. }
  13. return false;
  14. }
  15.  
  16. @org.junit.Test
  17. public void test() {
  18. int[] nums1 = { 1, 2, 3, 1 };
  19. int[] nums2 = { 1, 0, 1, 1 };
  20. int[] nums3 = { 1, 2, 3, 1, 2, 3 };
  21. int k1 = 3;
  22. int k2 = 1;
  23. int k3 = 2;
  24. System.out.println(containsNearbyDuplicate(nums1, k1));
  25. System.out.println(containsNearbyDuplicate(nums2, k2));
  26. System.out.println(containsNearbyDuplicate(nums3, k3));
  27. }
  28. }

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. git 删除错误commit

    其实就是硬reset到之前对的commit记录然后强行再推送到远程库一下 具体操作: git reset --hard <commit_id> git push origin HEAD - ...

  2. xml---基础了解

    XML 被设计用来传输和存储数据. HTML 被设计用来显示数据. 什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language). XML 是一种很像HTML的标 ...

  3. (尚006)Vue计算属性之set与get

    test004.html <!DOCTYPE html><html lang="en"><head> <meta charset=&quo ...

  4. BZOJ 3625:小朋友和二叉树 多项式开根+多项式求逆+生成函数

    生成函数这个东西太好用了~ code: #include <bits/stdc++.h> #define ll long long #define setIO(s) freopen(s&q ...

  5. java学习笔记(2)注释、public lcass、class、标识符、字面值、变量

    java学习笔记(1)中说过了java的一些基础知识,发展史,特点,编译和运行过程,配置环境变量等,接下来开始介绍java的语法等只是点  关于java源程序中的注释: *什么是注释?注释的作用是什么 ...

  6. 如何查询数据库中所有表格,或者查询是否存在某个表格-mysql和oracle

    这个问题,在之前就有写过,但是想找到语句还是记不得,这里主要提及我自己有用到的数据库mysql和oracle 1.mysql 这个是自己安装的,所有配置都是默认配置没有改变,所以保存表名的表还是inf ...

  7. C++标准库分析总结(八)——<仿函数、适配器、istream_iterator、ostream_iterator、bind>

    一.仿函数定义 仿函数是STL中最简单的部分,存在的本质就是为STL算法部分服务的,一般不单独使用.仿函数(functors)又称为函数对象(function objects),虽然函数指针虽然也可以 ...

  8. GO语言Error处理

    Go语言没有提供像Java.C#.Python语言中的try...catch异常处理方式,而是通过函数返回值逐层往上抛.好处就是避免漏掉本应处理的错误.坏处是代码啰嗦. 错误与异常区别 错误指的是可能 ...

  9. 无法将“Scaffold-DbContext”项识别为 cmdlet、函数、脚本文件或可运行程序的名称...

    原文链接:https://my.oschina.net/taadis/blog/889560 为什么80%的码农都做不了架构师?>>>     PM> Scaffold-DbC ...

  10. ubuntu之路——day4(今天主要看了神经网络的概念)

    感谢两位老师做的免费公开课: 第一个是由吴恩达老师放在网易云课堂的神经网络和深度学习,比较偏理论,使用numpy包深入浅出的介绍了向量版神经网络的处理方式,当然由于视频有点老,虽然理论很好但是工具有点 ...