每天一算: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. Spring Boot学习笔记——搭建一个最简单的hello world

    使用Spring Initializer新建项目 进入https://start.spring.io/新建一个项目,并下载下来. 这就是一个最基础的spring boot项目了. 我这里是基于spri ...

  2. 【Leetcode_easy】771. Jewels and Stones

    problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...

  3. iOS技术面试03:Foundation

    是否可以把比较耗时的操作放在NSNotificationCenter中 如果在异步线程发的通知,那么可以执行比较耗时的操作: 如果在主线程发的通知,那么就不可以执行比较耗时的操作 3.Foundati ...

  4. 20190923-基于Python3的RobotFramework的配置是初次使用

    因为最近改自动化框架在网上找了很多框架,发现RobotFramework不错,但是网上的资料很杂,现在将自己配置框架的学习笔记分享 Python配置RobotFramework的seleniumlib ...

  5. python 优雅的解析 jsonp

    一段 jsonp 格式数据 mtopjsonpweexcb1({"api":"mtop.taobao.idle.recycle.nextspunav.get", ...

  6. 洛谷 题解 UVA1151 【买还是建 Buy or Build】

    [题意] 平面上有\(n(n<=1000)\)个点,你的任务是让所有n个点联通.为此,你可以新建一些边,费用等于两个端点的欧几里得距离平方.另外还有\(q(q<=8)\)个套餐可以购买,如 ...

  7. IDEA使用 maven 搭建 SSM 框架

    文章目录 pom 文件的编写 项目结构 SSM 配置文件的编写 web.xml 的配置 总结 公司有个小的内部使用的软件,让开发,自己选择使用 SSM :因为之前自己学过,本以为一切水到渠成,但是好久 ...

  8. mosquitto安装遇到问题和解决办法

    问题1 make编译报错,提示xsltproc命令未找到 解决办法: yum  install libxslt 问题2 make编译报错,提示: failed to load external ent ...

  9. time() 函数时间不同步问题

    1.时区设置问题 处理方法:编辑php.ini  搜索 “timezone” 改写为 PRC 时区 2.服务器时间不同步 处理方法:设置服务器时间和本地时间进行同步

  10. Oracle数据库——用户(USER)

    前言   本文将介绍小白入门级别的关于用户的SQL语句.想全面了解的请参考其他资料.例如:Oracle官方文档--CREATE USER 文章目录 前言 创建用户 修改用户密码 授予用户权限 删除用户 ...