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

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. Spring STS 开发IDE下载安装

    https://spring.io/tools https://spring.io/tools3/sts/all/ Spring STS,全称是SpringSource Tool Suite ,是由s ...

  2. java Scanner输入数字、字符串

    package java05; import java.util.Scanner;//1.导包 /* Scanner类的功能,可以实现键盘输入数据,到程序当中 引用类型的一班使用步骤: 1.导包 2. ...

  3. 再谈lmbench

    摸了一轮ltp-ddt 再回头来看lmbench bandwidth & latency合集小王子 用起来确实方便. 只是官网显示的用法是: Go to the top directory, ...

  4. jenkins持续集成(一): 在Linux下的安装与配置

    撸了今年阿里.网易和美团的面试,我有一个重要发现.......>>> 前提: 安装了JDK,并配置好环境变量:JAVA_HOME 安装了Maven,并配置好环境变量:MAVEN_HO ...

  5. P与C

    P是排列:与次序有关,P(5.3)=5*4*3 C是组合:与次序无关,C(5.3)=(5*4*3)/(3*2*1)

  6. MetaException(message:For direct MetaStore DB connections, we don't support retries at the client level.)

    在mysql中执行以下命令:  drop database hive;  create database hive;  alter database hive character set latin1 ...

  7. ivew 绑定时间控件

    <FormItem label="开始时间" style="width: 100%" prop="startDate"> < ...

  8. JDBC简单总结

    几种常用数据库的JDBC URL 对于 Oracle 数据库连接,采用如下形式: jdbc:oracle:thin:@localhost:1521:sid 对于 SQLServer 数据库连接,采用如 ...

  9. Vue通信的10种方式

    1.10种通信方式 10种:https://juejin.im/post/5bd18c72e51d455e3f6e4334 2.除此之外,还有children和ref. 需要注意 $children  ...

  10. webpack 图片文件处理loader

    目录结构: 引用图片: body { /*background: red;*/ /*background: url("../img/test2.jpg"); 小图片*/ backg ...