[LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
描述
给出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)的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- C#解leetcode 219. Contains Duplicate II
该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...
- [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 ...
- 219 Contains Duplicate II 存在重复 II
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使 nums [i] = nums [j],并且 i 和 j 的绝对差值最大为 k. 详见:https://leetcod ...
- LeetCode 219 Contains Duplicate II
Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...
- 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 ...
- Leetcode 219 Contains Duplicate II STL
找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...
随机推荐
- mysql无法导入函数和存储过程解决方法
1. mysql> SET GLOBAL log_bin_trust_function_creators = 1; 2. 系统启动时 --log-bin-trust-function-creat ...
- 自定义string类
#include <iostream> #include <cstring> using namespace std; class String; class Data{ // ...
- 爆库记录(X-Forwarded-For注入漏洞实战 记录)
环境位置: https://www.mozhe.cn/bug/detail/QWxmdFFhVURDay90L0wxdmJXSkl5Zz09bW96aGUmozhe 参考: http://www.fr ...
- (八)Centos之文件搜索命令locate
一.文件搜索命令locate locate优点是 搜索速度快 ,缺点是只能按文件名搜索: 1.1 新建一个文件 1.2 更新数据库 locate命令搜索的是 /var/lib/mlocate 下的 ...
- 第十三章 RememberMe——《跟我学Shiro》
转发地址:https://www.iteye.com/blog/jinnianshilongnian-2031823 目录贴:跟我学Shiro目录贴 Shiro提供了记住我(RememberMe)的功 ...
- IntelliJ IDEA 简单设置
1.在idea中添加try/catch的快捷键 ctrl+alt+t 2.主题修改 选择菜单栏“File--settings--apperance--theme”,主题选择Darcula: 3.代码字 ...
- printf特殊用法
printf("%*.*lf\n", a, b, c); //表示a宽距,保留b位小数 用这种方法可以通过输入控制a和b
- 说一说Unsafe魔法类
这篇算是对 Unsafe 的一个总体概况,由于内容实在太多,后续会分开几篇文章对里面内容展开细讲 前言 Unsafe可以说是java的后门,类似西游记中的如来佛祖法力无边,Unsafe主要提供一些用于 ...
- 多年经验【Parallels Desktop14.0.1 永久激活 】版 推荐苹果mac 虚拟机pmg序列号
parallels desktop 14 mac 激活码 parallels 13免费密钥 parallels desktop 14 激活码 很多用 MAC 的朋友发现平时离不开 W ...
- yum 安装 epel-release 后出现yum doesn’t have enough cached data to continue错误的解决方案
工作中需要部署docker,由于是内网环境,无法直接访问外网,于是考虑在内网搭建yum私有源进行安装,内网服务器操作系统为centos 7.4.根据docker的官方安装方式进行安装时,要求安装 ep ...