给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使 nums [i] = nums [j],并且 i 和 j 的绝对差值最大为 k。

详见:https://leetcode.com/problems/contains-duplicate-ii/description/

Java实现:

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

C++实现:

class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
int size=nums.size();
if(size==0||nums.empty())
{
return false;
}
unordered_map<int,int> m;
for(int i=0;i<size;++i)
{
if(m.find(nums[i])!=m.end()&&i-m[nums[i]]<=k)
{
return true;
}
m[nums[i]]=i;
}
return false;
}
};

219 Contains Duplicate II 存在重复 II的更多相关文章

  1. [LeetCode] 219. Contains Duplicate II 包含重复元素 II

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  2. 217/219. Contains Duplicate /Contains Duplicate II

    原文题目: 217. Contains Duplicate 219. Contains Duplicate II 读题: 217只要找出是否有重复值, 219找出重复值,且要判断两者索引之差是否小于k ...

  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. 219. Contains Duplicate II【easy】

    219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...

  5. 219. Contains Duplicate II - LeetCode

    Question 219. Contains Duplicate II Solution 题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false 思路:用一个map记录每 ...

  6. [LeetCode] 220. Contains Duplicate III 包含重复元素 III

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

  7. [LeetCode] 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之回溯法专题-90. 子集 II(Subsets II)

    Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...

  9. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

随机推荐

  1. apache下配置认证用户

    有时候我们须要给我apacheserver下制定的文件夹加上用户认证,方便一些而用户进行文件的浏览.配置例如以下: 1 设置用户 1 htpasswd -c file_path user_name 回 ...

  2. 程序运行中(BSS段、数据段、代码段、堆栈)

    程序运行中(BSS段.数据段.代码段.堆栈) BSS段:(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文Block Started by Symbol的简 ...

  3. Python中解决中文乱码问题

    乱码原因:因为你的文件声明为utf-8,并且也应该是用utf-8的编码保存的源文件.但是windows的本地默认编码是cp936,也就是gbk编码,所以在控制台直接打印utf-8的字符串当然是乱码了. ...

  4. Cookie防伪造防修改 电商课题:cookie防篡改

    主要防止非法用户修改cookie信息,以及cookie的超时时间 传统cookie存储,Cookie(name, value),value很容易就被篡改. 防修改cookie存储,Cookie(nam ...

  5. 通过SQL SERVER加入系统管理员帐号

    通过SQL SERVER加入系统管理员帐号.当然是须要有足够的权限,远程链接数据库后执行脚本,脚本例如以下: /* 此代码是在master数据库下执行 添加系统管理员:mmcgzs password: ...

  6. Linux_C——动态库,静态库

    /usr/lib /lib:标准系统库文件          库是一组预先编译好的函数的集合,这些函数都是按照可重用的原则编写的.它们通常有一组相互关联的函数组成以          执行某项常见的任 ...

  7. 图像处理之基础---很好的一个快速比较两副图片是否相同的code 可用于公安鉴别

    转自Codeproject http://www.codeproject.com/dotnet/comparingimages.asp Public Enum CompareResult ciComp ...

  8. 【iOS系列】-UIImageView帧动画相关属性介绍

    UIImageView帧动画相关属性介绍 1:相关属性: //An array of UIImage objects to use for an animation.存放UIImage对象,会按顺序显 ...

  9. ibatis和mybatis的区别

    区别1:全局配置文件(sqlMapConfig.xml)的差异 主要是元素标签命名的差异,比如mybatis的根元素标签为<configuration>,ibatis的 根元素标签为< ...

  10. centos7下tomcat7 或tomcat8启动超慢原因

    1,找到你的jdk安装的位置 ${JAVA_HOME}/jre/lib/security/java.security 2,vi 打开后找到 securerandom.source=file:/dev/ ...