每天一算:Contains Duplicate II

描述

给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k

Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true.

Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true

Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false

解析

考虑用滑动窗口与查找表来解决。

  • 设置查找表record,用来保存每次遍历时插入的元素,record的最大长度为k

  • 遍历数组nums,每次遍历的时候在record查找是否存在相同的元素,如果存在则返回true,遍历结束

  • 如果此次遍历在record未查找到,则将该元素插入到record中,而后查看record的长度是否为k + 1

  • 如果此时record的长度是否为k + 1,则删减record的元素,该元素的值为nums[i - k]

  • 如果遍历完整个数组nums未查找到则返回false

代码

public static boolean containsDuplicate2(int[] n, int k) {
if (n == null || n.length < k) {
return false;
}
List<Integer> list = new ArrayList<>(k);
for (int i = 0; i < n.length; i++) {
if (!list.contains(n[i])) {
list.add(n[i]); if (list.size() > k) {
list.remove(0);
}
} else {
return true;
}
}
return false;
}

[LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)的更多相关文章

  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. [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 ...

  3. LeetCode 219. Contains Duplicate II (包含重复项之二)

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

  4. C#解leetcode 219. Contains Duplicate II

    该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...

  5. [LeetCode] 219. Contains Duplicate II 解题思路

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

  6. 219 Contains Duplicate II 存在重复 II

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

  7. LeetCode 219 Contains Duplicate II

    Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...

  8. Java for LeetCode 219 Contains Duplicate II

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

  9. Leetcode 219 Contains Duplicate II STL

    找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...

随机推荐

  1. 【MM系列】SAP S/4 HANA的物料编码40位设置

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP S/4 HANA的物料编码4 ...

  2. eNSP——利用三层交换机实现VLAN间路由

    原理: VLAN将一个物理的LAN在逻辑上划分成多个广播域.VLAN内的主机间可以直接通信,而VLAN间不能直接互通. 在现实网络中,经常会遇到需要跨VLAN相互访问的情况,工程师通常会选择一些方法来 ...

  3. python不定长参数 *argc,**kargcs(19)

    在 python函数的声明和调用 中我们简单的了解了函数的相关使用,然而在函数传递参数的时候,我们埋下了一个坑,关于不定长参数的传递我们还没有讲,今天这篇文章主要就是讲解这个问题. 一.函数不定长参数 ...

  4. 学习笔记:oracle学习二:oracle11g数据库sql*plus命令之常用sqlplus命令、格式化查询结果

    目录 1.常用sqlplus命令 1.1 HELP命令 1.2 describe命令 1.3 SPOOL命令 1.4 其他常用命令 1.4.1 define命令 1.4.2 show命令 1.4.3 ...

  5. java Files 和 Path对文件操作

    1.拷贝文件 /** * 拷贝文件,生成新的文件名 * @param pathUpload * @return */ private String converUploadFileName(Strin ...

  6. [转帖]Apache、Tomcat与Catalina作为软件名字的含义与关系

    Apache.Tomcat与Catalina作为软件名字的含义与关系 -- :: 复杂度掠夺者 阅读数 3356更多 分类专栏: 术语解释 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA ...

  7. [转帖]超详细的EXPDP、IMPDP规范及常用技巧总结

    超详细的EXPDP.IMPDP规范及常用技巧总结 https://www.toutiao.com/i6727232212850180619/ 原创 波波说运维 2019-08-24 00:06:00 ...

  8. Docker从国内代理下载镜像

          docker从国内拉取镜像,或者通过加速器拉取         由于国内访问直接访问Docker hub网速比较慢,拉取镜像的时间就会比较长.一般我们会使用镜像加速或者直接从国内的一些平台 ...

  9. 利用Python进行数据分析_Numpy_基础_3

    通用函数:快速的元素级数组函数 通用函数,是指对数组中的数据执行元素级运算的函数:接受一个或多个标量值,并产生一个或多个标量值. sqrt 求平方根 np.sqrt(arr) exp 计算各元素指数 ...

  10. docker&git&gitlab-安装/部署/新建

    --安装gcc yum -y install gcc --安装g++ yum -y install gcc-c++ --安装编译所需的包 yum -y install curl-devel expat ...