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 jis at most k.

这个题目在前一个题的基础上有部分改动,对nums[i] = nums[j]出现的范围作出了规定,不是全局的,而是要求|i-j|<=k;

 public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer,Integer> map = new HashMap<>(0); for(int i=0; i<nums.length; i++){
if(map.containsKey(nums[i])){
if(i-map.get(nums[i])<=k) return true;
}
if(map.size()>k) map.remove(nums[i-k]);
map.put(nums[i],i);
}
return false;
}
}

【二刷】二刷和一刷的变动不是很大,但是对HashMap的了解更深了。

 public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
if(nums.length <= 1 || k == 0) return false; Map<Integer, Integer> map = new HashMap<>();
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;
}
}

【三刷】三刷的直接使用HashSet。遍历数组,如果当前下标已经大于k,则把set中不可能成立的数据删除掉,然后在把当前数字添加进去。如果set中已经包含该数,则会添加失败。

HashSet的add函数在添加成功返回true,在添加失败时返回false。

 public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Set<Integer> set = new HashSet<Integer>();
for(int i = 0; i < nums.length; i++){
if(i > k) set.remove(nums[i-k-1]);
if(!set.add(nums[i])) return true;
}
return false;
}
}

LeetCode OJ 219.Contains Duplicate 2的更多相关文章

  1. 【一天一道LeetCode】#219. Contains Duplicate II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. 【LeetCode】219. Contains Duplicate II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用set 使用字典 日期 题目地址:https:/ ...

  3. LeetCode OJ 220.Contains Duplicate 3

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  4. LeetCode OJ 217.Contains Duplicate

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  5. 【LeetCode】219. Contains Duplicate II

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  6. Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  7. LeetCode OJ:Contains Duplicate III(是否包含重复)

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  8. LeetCode OJ:Contains Duplicate(是否包含重复)

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

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

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

随机推荐

  1. 训练[2]-DFS

    题目A: 题目B[https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pro ...

  2. jQuery EasyUI的使用入门

    jQuery EasyUI不是什么太高级的东西,就是用jQuery写了很多方法.定义了很多属性,通过调用这些方法.属性,可以达到一些特定的效果,然后我们再根据具体需求微调就好了.至少需要导入两个样式表 ...

  3. Android:Asmack能登录但是获取不到联系人的问题

    先说一下:换了N个jar包,换了N个OpenFire,就是获取不到联系人.但是能登录.特别纳闷,百度不到,google一下,有人隐约说了下权限的问题. 终于搞出来了,总结一下,浪费了一整天的时间.(码 ...

  4. JVM内存模型,垃圾回收算法

    JVM内存模型总体架构图 程序计数器多线程时,当线程数超过CPU数量或CPU内核数量,线程之间就要根据时间片轮询抢夺CPU时间资源.因此每个线程有要有一个独立的程序计数器,记录下一条要运行的指令.线程 ...

  5. Strusts2--课程笔记9

    防止表单重复提交: 用户可能由于各种原因,对表单进行重复提交.Struts2中使用令牌机制防止表单自动提交.以下引用自北京动力节点: 前端表单提交代码: <%@ page language=&q ...

  6. iScroll屏幕滑动函数封装总结

    //iScroll.js屏幕滚动函数 function funScroll(a,b) { var d; function beforload() { d = new iScroll(a, { chec ...

  7. Directed Roads

    Directed Roads 题目链接:http://codeforces.com/contest/711/problem/D dfs 刚开始的时候想歪了,以为同一个连通区域会有多个环,实际上每个点的 ...

  8. UEditor的jQuery插件化

    UEditor本身并不依赖jQuery,但如果在项目中同时使用两者的话,可能会希望使用jQuery语法创建和获取编辑器实例.为此,需要为jQuery编写插件,代码如下: (function ($) { ...

  9. 第一百零二节,JavaScript函数

    JavaScript函数 学习要点: 1.函数声明 2.return返回值 3.arguments对象 函数是定义一次但却可以调用或执行任意多次的一段JS代码.函数有时会有参数,即函数被调用时指定了值 ...

  10. 2016中国大数据技术大会( BDTC ) 共商大数据时代发展之计

    中国大数据技术大会(BDTC)的前身是Hadoop中国云计算大会(HadoopinChina,HiC).从2008年仅60余人参加的技术沙龙发展到当下数千人的技术盛宴,目前已成为国内最具影响力.规模最 ...