Question

219. Contains Duplicate II

Solution

题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false

思路:用一个map记录每个数的坐标,如果数相同,如果坐标差小于k则返回true否则覆盖,继续循环

Java实现:

public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int tmp = nums[i];
if (map.get(tmp) != null) {
// System.out.println(i + "---" + map.get(tmp));
if (i - map.get(tmp) <= k) return true;
}
map.put(tmp, i);
}
return false;
}

219. Contains Duplicate II - LeetCode的更多相关文章

  1. 219. Contains Duplicate II【easy】

    219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...

  2. [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)

    每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...

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

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

  5. 【一天一道LeetCode】#219. Contains Duplicate II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. 【LeetCode】219. Contains Duplicate II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用set 使用字典 日期 题目地址:https:/ ...

  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. 什么是arduino及.arduino分类

    关于什么是arduino没有什么可说的,想要接触arduino多多少少的都会对arduino有一定的理解,我认为,arduino应该算是一个连接硬件与软件的平台,通过他,你可以将你的想法以代码的形式呈 ...

  2. 4. Git基本工作流程

    4. Git基本工作流程 Git工作区域 向仓库中添加文件流程

  3. (stm32f103学习总结)—DS18B20

    一. DS18B20简介 DS18B20数字温度传感器接线方便,封装后可应用于多种场合,如管道式,螺纹式,磁铁吸附式,不锈钢封装式.主要根据应用场合的不同而改变其外观.封装后的DS18B20可用于电缆 ...

  4. css-theme 通过一套源码生成一份包含多套皮肤配置的样式文件

    css-theme 通过单一css文件生成多套主题,并合并入一个css文件中 特性 只加载一个css,通过切换rootClass瞬间切换主题 体积压缩,将多套css合并,去除冗余代码,避免文件体积膨胀 ...

  5. 快如闪电,触控优先。新一代的纯前端控件集 WijmoJS发布新版本了

    全球最大的控件提供商葡萄城宣布,新一代纯前端控件 WijmoJS 发布2018 v1 版本,进一步增强产品功能,并支持在 Npm 上的安装和发布,极大的提升了产品的易用性. WijmoJS 是用 Ty ...

  6. Node的重要性

    一. 为什么要学Node 1. 是自己更全面, 有大局观 2. 提升话语权 3. 升职加薪的筹码 二. Node的作用和应用 1. 脱离浏览器运行 js 2. 后台API编写 3. webpack, ...

  7. Map集合的六种遍历方式

    学习目标: 熟练掌握Map的遍历方式 例题: 需求:遍历Map集合 代码如下: package com.yy.object.test.test_collection; import java.util ...

  8. 时间篇之centos7修复ntpq: read: Connection refused

    关于ntp同步时间, 由于是解决问题,所以理论性内容不多. 关于UTC NTP要提供准确的时间,就必须有准确的时间来源,那可以用格林尼治时间吗?答案是否定的. 因为格林尼治时间是以地球自转为基础的时间 ...

  9. linux中LNMP架构和location用法

    location 使用Nginx Location可以控制访问网站的路径,但一个server可以有多个location配置, 多个location的优先级该如何区分 location匹配符号 匹配符 ...

  10. 都2022年了,HDFS为何还如此能战!

    摘要:HDFS也许不是最好的大数据存储技术,但依然是最重要的大数据存储技术. 本文分享自华为云社区<HDFS为何在大数据领域经久不衰?>,作者: JavaEdge. 1.概述 1.1 简介 ...