Question

219. Contains Duplicate II

Solution

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

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

Java实现:

  1. public boolean containsNearbyDuplicate(int[] nums, int k) {
  2. Map<Integer, Integer> map = new HashMap<>();
  3. for (int i = 0; i < nums.length; i++) {
  4. int tmp = nums[i];
  5. if (map.get(tmp) != null) {
  6. // System.out.println(i + "---" + map.get(tmp));
  7. if (i - map.get(tmp) <= k) return true;
  8. }
  9. map.put(tmp, i);
  10. }
  11. return false;
  12. }

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. 10本 Linux PDF 书籍免费分享

    本篇文章主要分享以下Linux开发PDF书籍 一.Linux程序设计二.Unix环境高级编程三.Unix_Linux编程实践教程四.鸟哥的私房菜五.深入理解Linux内核六.Linux命令行与shel ...

  2. 5_系统的可控性_Controllability

  3. Solution Architect

    Solution Architect How to become a solution architect - CareerExplorer What does a solution architec ...

  4. 判断页面环境是否在小程序的webview中

    最近公司需要做小程序项目,但是又希望能够快速开发,就想着把web端的响应式页面放到webview里快速开发.但在判断页面环境的时候出现一些问题. 环境问题 用小程序提供的wx.miniProgram. ...

  5. Web缓存总结

    web缓存作用 减少网络带宽消耗降低服务器压力减少网络延迟,加快页面打开速度 Web缓存的类型 数据库数据缓存:为了提供查询的性能,会将查询后的数据放到内存中进行缓存,下次查询时,直接从内存缓存直接返 ...

  6. 前端眼里的docker

    docker是什么 可以简单的认为docker容器是一个虚拟机,封装就是把这个虚拟机打包,打包后能在任何系统跑,docker装上即用.也可以形象的比喻成一个集装箱,把所有货物都打包好放到箱子里,不需要 ...

  7. Java中重载的应用

    学习目标: 掌握Java方法的重载 学习内容: 1.重载定义 参数列表: 参数的类型 + 参数的个数 + 参数的顺序 方法签名: 方法名称 + 方法参数列表,在同一个类中,方法签名是唯一的,否则编译报 ...

  8. c++对c的拓展_编译检测的增强

    一:全局变量检测增强:c++编译对全局变量的声明定义有严格区别 //c中全局变量的声明及定义         //c++中全局变量的声明及定义        //c++全局变量写成下列形式则报错 in ...

  9. Go xmas2020 学习笔记 00-03、Basic Types

    00-02-Hello Example. 目录结构. 不一样的Hello World. 巧妙的单元测试. 传入os.Args切片. go mod init. 03-Basic Types. 变量类型与 ...

  10. 防抖-小程序-input输入频繁时搜索出bug

    html: <input type="text" class="input_search" placeholder="搜索周边店铺"  ...