[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 ...
随机推荐
- java的servlet执行过程是怎么样的?
java的servlet执行过程是怎么样 答: Servlet执行过程:程序第一次访问,会调用servlet的init()方法初始化(只执行一次),每次程序执行都会根据请求调用doGet()或者d ...
- 多线程分页处理批量数据--jdbc方式
/** * 同步数据信息到ES * @return */ public boolean syncNhReportSeessToEs(){ long begin = System.currentTime ...
- Block pool ID needed, but service not yet registered with NN java.lang.Exception: trace 异常解决
以上为报错信息: 原因大概为:dd和nd关联的versionId不同导致, 解决方案,备份之前的current文件夹,让其自己生成新的.
- Python3 中codecs进行文件的读取
简单的概念与说明 编码(动词):按照某种规则(这个规则称为:编码(名词))将"文本"转换为"字节流".而在python 3中则表示:unicode变成str 解 ...
- HmacSHA256算法(C# 和 Java)
Java代码: /** * HmacSHA256算法,返回的结果始终是32位 * @param key 加密的键,可以是任何数据 * @param content 待加密的内容 * @return 加 ...
- 【Leetcode_easy】1078. Occurrences After Bigram
problem 1078. Occurrences After Bigram 题意 solution: class Solution { public: vector<string> fi ...
- [CareerCup] 2. Bomberman 炸弹人
We have a 2D grid. Each cell is either a wall, an enemy or empty. For example (0-empty, X-enemy, Y-w ...
- 生成对抗网络GAN详解与代码
1.GAN的基本原理其实非常简单,这里以生成图片为例进行说明.假设我们有两个网络,G(Generator)和D(Discriminator).正如它的名字所暗示的那样,它们的功能分别是: G是一个生成 ...
- Docker 容器资源隔离 namespace(十)
目录 一.简介 Linux Namespace的6大类型 二.Mount Namespace 三.IPC Namespace 四.Network Namespace 五.UTS Namespace 六 ...
- Extjs editor 设置默认值
一.前言 Ext js 给 editor 设置默认值用 value 无效,在 Model 中添加 defaultValue 即可. 二.实例 view: Ext.define('xxxx.view.P ...