[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设置北京时间的时区
java设置北京时间的时区 解决方法: 设置北京时间的时区,消除时间差. TimeZone timeZone = TimeZone.getTimeZone("GMT+8"); ...
- Qt编写控件属性设计器2-拖曳控件
一.前言 上一篇文章把插件加载好了,并且把插件中的所有控件都显示到了列表框中,这次要做的就是实现拖曳控件的功能,用户选择一个控件拖曳到画布上,松开,在松开位置处自动实例化该控件,这个需要用到dropE ...
- @Qualifier is not applicable for constructor
问题场景: 笔者在springboot项目中使用java_websocket作为客户端,调用第三方ws服务. 最初只调用一个ws服务,以下代码可以正常工作: @Bean public URI ttsU ...
- Delphi中动态加载TreeView信息
unit Unit3; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- git命令手册
以下内容是我在学习和研究Git时,对Git操作的特性.重点和注意事项的提取.精练和总结,可以做为Git操作的字典,方便大家查阅: 备注:本文会不断更新完善: 目录 一. 语法格式描述 二. git环境 ...
- 创建.Net Core For WPF项目并且添加VS Code编译运行支持
1.下载最新的Visual Studio 2019或者Preview版本,新建项目,找到"WPF App(.Net Core)"模板. 2.输入项目名称"NetCore- ...
- php验证码实例
验证码实例如下看不懂的先看gd库的那一篇,session不懂的看from提交的数据都哪里了那一篇,欢迎关注 index.php <!DOCTYPE html> <html lang= ...
- C/C++查漏补缺(常更)
一.#define宏定义 如下程序段,则输出结果为: #define MAX 12 int main(){ cout << "20\0MAX019" << ...
- (生鲜项目)07. api view实现商品列表页
第一步: 环境配置 1. DRF官网: https://www.django-rest-framework.org/ 仔细查看自己当前的python版本以及django版本是否支持DRF, 然后就看看 ...
- 使用pycharm调试django 打断点调试后台代码
第一步 第二步 第三步 第四步 Script path:C:\pythonProject\Code\mysite\manage.py Parameters: runserver 远程访问的话 远程调试 ...