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. C++中int转为char 以及int 转为string和string 转int和空格分隔字符串

    1.对于int 转为char 直接上代码: 正确做法: void toChar(int b) { char u; ]; _itoa( b, buffer, ); //正确解法一 u = buffer[ ...

  2. 获取所有input值 处理成json格式再利用$.post提交

    <script>$(document).ready(function(){        $("#sub").click(function(){        var ...

  3. KVM guest caching modes

    kvm中host和guest各自维护自己的page caches,使得内存中有两份缓存数据.host的缓存为page cache可以理解为读缓存,guest的缓存为disk write cache,可 ...

  4. Nohttp框架在Android Studio中的使用

    1.添加noHttp的使用权限 <!-- 读写 sd 卡 --> <uses-permission android:name="android.permission.REA ...

  5. vcs 下使用system verilog调用c函数

    c中要加入<svdpi.h> sv的tb中加入 import "DPI-C" function int funcname(); 仿真时,vcs命令行中加入 +vc fu ...

  6. Maven 复制jar到指定目录

    在完成模块开发后,需要发布jar到nexus上,与此同时,则要部署开发的模块,需要将编译打包的jar复制到指定的路径,再进行部署,而不是手动的去复制那些jar,因为当模块多的话,则会感到特别的烦,所以 ...

  7. English Vocabulary

    Creative - producing or using original and unusual ideas Computer literate - familiarity with comput ...

  8. 一些Wifi破解姿势

    wlan0:无线网卡设备 BSSID/AP's MAC:目标路由器的mac地址 Client's MAC:连接到此wifi客户端的mac地址 ESSID:这个无线的名字 大致思路: 获取bssid和e ...

  9. Eclipse Kepler maven工程配置JDK1.8

    首先需要下载插件:"Help" --> "Market Place" --> Search for java 8 kepler. install J ...

  10. qt rcc 使用

    做项目的时候, 最初把图片放到 qrc里面, 使用编译生成的qrc_cpp. 但是编译超慢, 还经常提示"编译器空间不足". 网上很多人说是 中文路径的问题. 可是总是感觉编译器空 ...