LeetCode OJ 219.Contains Duplicate 2
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的更多相关文章
- 【一天一道LeetCode】#219. Contains Duplicate II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】219. Contains Duplicate II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用set 使用字典 日期 题目地址:https:/ ...
- 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 ...
- LeetCode OJ 217.Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- 【LeetCode】219. Contains Duplicate II
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- 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 ...
- 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 ...
- LeetCode OJ:Contains Duplicate(是否包含重复)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
随机推荐
- iOS开发工具箱
工欲善其事 必先利其器 最新重新安装了MacBook Air和Mac Pro的系统,彻底解决了一编译就报硬盘空间不足的问题.平时开发中比较常用的工具都需一一安装. XCode:没什么好说的,外星人都知 ...
- 如何在Sublime Text中添加代码片段
我们在编写代码的时候,总会遇到一些需要反复使用的代码片段.这时候就需要反复的复制和黏贴,大大影响效率.我们利用Sublime Text的snippet(代码片段)功能,就能很好的解决这一问题.通俗的讲 ...
- HDU 2601 An easy problem
(i+1)*(j+1)=n+1 转换成上面这个式子,也就是问n+1的因子有几个 #include<cstdio> #include<cstring> #include<c ...
- ACdream 1063 平衡树
写的很丑的字典树.听王大神的话 需要改进. #include<stdio.h> #include<string.h> #include<math.h> #incl ...
- js-轮播图
<!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" charset ...
- ZOJ Martian Addition
Description In the 22nd Century, scientists have discovered intelligent residents live on the Mars. ...
- 关于ActionScript在Java调用上的一些原理
在公司遇到了ActionScript调用Java的需求,所以大概了解了一下: 一般基本是分成了三块,本身flash的项目,ActionScript的库,Java的库 通信方式一般有两种: 一.Acti ...
- java 导出excel(读数据库案例)
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.S ...
- kettle 连接Hadoop 遇错
kettle从windows中往hdfs中写文件
- OAuth流程
简介 OAuth是一种协议,OAuth协议为用户资源的授权提供了一个安全的.开放而又简易的标准 第三方若想访问用户资源,就必须遵守服务提供商实现的OAuth协议 OAuth授权的步骤(新浪微博为例) ...