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

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. 12-低延迟、全接口(HMDI、DVI、YPb Pr、RGB)H.264全高清编码器解码器

    低延迟.全接口(HMDI.DVI.YPb Pr.RGB)H.264全高清编码器解码器 一.产品介绍  1.近零延时的H.264压缩到1920x1080p60  该产品提供分辨率为1920x1080p6 ...

  2. SYSAUX表空间满,

    step1.  确认到底是哪个段占用了sysaux空间: select segment_name,sum(bytes)/1024/1024 from dba_segments where tables ...

  3. ThreadPoolExecutor扩展

    import java.util.concurrent.*; /** * ThreadPoolExecutor扩展 */ public class ExtThreadPool { public sta ...

  4. 超微主板IPMI的使用

    https://blog.nicky1605.com/supermicro-motherboards-use-ipmi.html IPMI(智能平台管理接口)现在大部分都是集成到主板上了,我们利用IP ...

  5. PHP curl_multi_close函数

    curl_multi_close — 关闭一组cURL句柄 说明 void curl_multi_close ( resource $mh ) 关闭一组cURL句柄. 参数 mh 由 curl_mul ...

  6. Git中的分支

    具体请参考:https://git-scm.com/book/zh/v1/Git-%E5%88%86%E6%94%AF-%E4%BD%95%E8%B0%93%E5%88%86%E6%94%AF Git ...

  7. javascript is ths best computer language

    alert('javascript is one of the best computer languages')

  8. html 的一些基础操作

    花了一天学了点html语言..不记下来的话又白学了 基础中的基础格式 <!DOCTYPE html> <html> <head> <!-- 字符集的选择 ut ...

  9. 牛客多校训练营第九场 J - Symmetrical Painting (排序)

    J - Symmetrical Painting 题意 给你\(n\)个矩形, 左下角\((i-1,\ L_i)\), 右上角\((i,\ R_i)\), 找一条线\(l\)平行于\(x\)轴, 让这 ...

  10. AT2705 Yes or No(组合数学)

    传送门 解题思路 首先将这个模型放到坐标轴上,\(x\)轴表示\(1\),\(y\)轴表示\(0\).问题就转化成了从\((0,0)\)走到\((n,m)\),每次可以猜测向\(x\)轴或向\(y\) ...