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

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. vue,一路走来(10)--生产环境

    生产环境下的一些问题 使用webpack 打包前端应用后,图片和css.js 资源引用会出问题,这源于开发环境的目录和生产环境的路径[url]不同 比如,开发环境的url是:http://localh ...

  2. 二、TortoiseSVN 合并、打分支、合并分支、切换分支

    一.合并 点击Edit conflict来编辑冲突: 在合并后的枝干对应栏中编辑后,Save保存后关闭. 二.TortoiseSVN 打分支.合并分支.切换分支 1.SVN打分支 方式一:先检出,再打 ...

  3. Codeforces Round #393 (Div. 2) - A

    题目链接:http://codeforces.com/contest/760/problem/A 题意:给定一个2017年的月份和该月的第一天的星期,问该月份的日历表中需要多少列.行有7列表示星期一~ ...

  4. JVM参数说明

    转载于https://www.cnblogs.com/redcreen/archive/2011/05/04/2037057.html文章 JVM参数说明 -Xms:初始堆大小  默认值=物理内存的1 ...

  5. 接口上传base64编码图片

    package com.*.util; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io. ...

  6. cmd美化

    原本的cmd 虽然原本的cmd很简约黑底白字,但是看久了也会视觉疲劳 美化(丑化) 打开cmd右键头部选择属性 字体选项这里可以修改字体的大小和选择字体,修改之后下方会有预览,颜色选项这里点选屏幕文字 ...

  7. C++ 彩色图像(RGB)三通道直方图计算和绘制,图像逆时针旋转90° 实现代码

    #include "iostream" #include "opencv2/opencv.hpp" #include "vector" us ...

  8. 03 spring security执行流程分析

    spring security主要是依赖一系列的Filter来实现权限验证的,责任链设计模式是跑不了的.下面简单记录一下spring操作这些Filter的过程. 1. WebSecurityConfi ...

  9. php strcasecmp()函数 语法

    php strcasecmp()函数 语法 作用:比较两个字符串(不区分大小写)直线电机驱动器 语法:strcasecmp(string1,string2) 参数: 参数 描述 string1 必须, ...

  10. Codeforces 842C--Ilya And The Tree(dfs+树)

    原题链接:http://codeforces.com/contest/842/problem/C 题意:一个以1为根节点的树,每个节点有一个值ai,定义美丽度:从根节点到这个节点的路径上所有ai的gc ...