---恢复内容开始---

Description


Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

Example 1:

Input: nums = [,,,], k =
Output: true

Example 2:

Input: nums = [,,,], k =
Output: true

Example 3:

Input: nums = [,,,,,], k =
Output: false

问题描述:给定一个数组和一个整数k 判断是否存在两个重复元素i,j 使i和j的差的绝对值不大于k 如果存在 返回true 否则返回false

思路,这里使用字典Dictionary保存元素和元素的索引。如果能找到元素,则计算当前元素和上一个元素的索引的差是否不大于k

public bool ContainsNearbyDuplicate(int[] nums, int k) {
Dictionary<int,int> dic = new Dictionary<int,int>();
for(int i = ; i < nums.Length; i++){
if(dic.ContainsKey(nums[i])){
if(i - dic[nums[i]]<=k)
return true;
}
dic[nums[i]] = i;
}
return false;
}

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

  1. LeetCode Array Easy 217. Contains Duplicate

    Description Given an array of integers, find if the array contains any duplicates. Your function sho ...

  2. LeetCode Array Easy 167. Two Sum II - Input array is sorted

    Description Given an array of integers that is already sorted in ascending order, find two numbers s ...

  3. 【leetcode❤python】 219. Contains Duplicate II

    #-*- coding: UTF-8 -*-#遍历所有元素,将元素值当做键.元素下标当做值#存放在一个字典中.遍历的时候,#如果发现重复元素,则比较其下标的差值是否小于k,#如果小于则可直接返回Tru ...

  4. 219. Contains Duplicate II【easy】

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

  5. 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II

     217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...

  6. 219. Contains Duplicate II - LeetCode

    Question 219. Contains Duplicate II Solution 题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false 思路:用一个map记录每 ...

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

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

  8. (easy)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 包含重复元素 II

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

随机推荐

  1. rpm2cpio - 从 RPM 软件包中提取 cpio 归档

    SYNOPSIS rpm2cpio [filename] DESCRIPTION rpm2cpio 将指定的一个 .rpm 文件转换为一个 cpio 文档,输出到标准输出.如果给出了 `-' 参数,那 ...

  2. shell 单行多行注释

    1. 单行注释 众所周知,#  比如想要注释:echo “ni” # echo "ni" 2. 多行注释: 法一: : << ! 语句1 语句2 语句3 语句4 ! 法 ...

  3. Synchronized和ReentranLock的比较

    并发编程最容易遇到的问题就是就是安全问题,因此解决方式有两种 使用同步方法或同步代码块(Synchronized关键字) 使用锁机制(ReentranLock) 同步方法和同步代码块(Synchron ...

  4. 关于touch-action

    在项目中发现 ,Android下列表页的滚动加载失效. 原因: css中设定了html{ touch:none } 解决方法:移除该样式. touch:none // 当触控事件发生在元素上是时,不进 ...

  5. 3.1.2 Socket网络通信开发

    Socket语法 Python中,我们用Socket()函数来创建套接字,语法如下: socket.socket([family[, type[, proto]]]) 参数 family:套接字家族可 ...

  6. oracle for loop

    我们在Oracle存储过程中需要遍历一张表,应该怎样做.我想大多少的人第一个念头就是Cursor. 比如: create or replace procedure StudyCursor( resul ...

  7. note4

  8. Java Web学习总结(9)学习总结-JSTL

    一,JSTL概述 JSTL(JSP Standard Tag Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能.jstl出现的目的同el一样也是要代替jsp页 ...

  9. SSM项目用ajax来显示数据

    <script type="text/javascript"> //1:页面加载完成后,直接去发送ajax请求,要到分页的数据 $(function(){ $.ajax ...

  10. vue打包配置发布路径

    目的:配置路径,让打包后的dist在本地可以打开. 方法:修改build文件夹下边的的webpack.dev.conf.js文件,找到devServer下边的publicPath,这个来源于confi ...