LeetCode——Contains Duplicate II
Description:
Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<nums.length; i++) { if(map.containsKey(nums[i])) {
int j = map.get(nums[i]);
if(i-j<=k) {
return true;
}
else {
map.remove(nums[j]);
map.put(nums[i], i);
}
}
else {
map.put(nums[i], i);
}
}
return false;
}
}
LeetCode——Contains Duplicate II的更多相关文章
- [leetcode] Contains Duplicate II
Contains Duplicate II Given an array of integers and an integer k, find out whether there there are ...
- [LeetCode] Contains Duplicate(II,III)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- [LeetCode] Contains Duplicate II 包含重复值之二
Given an array of integers and an integer k, return true if and only if there are two distinct indic ...
- LeetCode Contains Duplicate II (判断重复元素)
题意:如果有两个相同的元素,它们之间的距离不超过k,那么返回true,否则false. 思路:用map记录每个出现过的最近的位置,扫一边序列即可.扫到一个元素就判断它在前面什么地方出现过.本题数据有点 ...
- leetcode Contains Duplicate II python
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- LeetCode & Q219-Contains Duplicate II
Array Hash Table Description: Given an array of integers and an integer k, find out whether there ar ...
- leetcode:Contains Duplicate和Contains Duplicate II
一.Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your fun ...
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II
1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...
随机推荐
- 部署zookeeper集群
1.把zookeeper.tar.gz解压之后,移动到/usr目录下 2.首先要给zookeeper之间的每个节点的ssh设置无密码登陆 3.在zookeeper目录下编辑zoo.cfg,复制zoo_ ...
- innobackupex参数之 --throttle 限速这个值设置多少合理 原创
innobackupex参数之--parallel --throttle--parallel 此参数用于开启多个子进程并发备份多个数据文件(注意,一个数据文件只会有一个进程完成备份).可以加快备份速度 ...
- Hibernate- 子查询
01.搭建开发环境 02.子查询 package com.gordon.test; import java.util.List; import org.hibernate.Session; impor ...
- vmware下ubuntu不能上网 => 恢复默认虚拟网络
1.关闭虚拟机ubuntu 2.打开:编辑=> 虚拟网络编辑器 3.打开后点击左下角恢复默认 4.重启ubuntu就可以了
- 传说中的纯CSS圆角代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- mysql数据库要按当天、昨天、前七日、近三十天、季度、年查询
mysql数据库要按当天.昨天.前七日.近三十天.季度.年查询
- linux安装ant
1.从http://ant.apache.org 上下载tar.gz版ant 2.复制到/usr下 3.tar -vxzf apahce-ant-1.9.2-bin.tar.gz 解压 4.chow ...
- Unity+NGUI性能优化方法总结
1 资源分离打包与加载 游戏中会有很多地方使用同一份资源.比如,有些界面会共用同一份字体.同一张图集,有些场景会共用同一张贴图,有些会怪物使用同一个Animator,等等.可以在制作游戏安装包时将这些 ...
- 在工程名.h头文件中写public:
class CaccessimageApp : public CWinApp { public: _ConnectionPtr m_pConnection; CaccessimageApp(); // ...
- OpenCV学习:实现简单的图像叠加
本实例使用简单的线性叠加方法来实现两幅图像的叠加,主要使用的知识如下: 1)线性融合 2)addWeighted函数 //! computes weighted sum of two arrays ( ...