难度:easy

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.

思路:找到array中是否存在两个数,这两个数相同,且index之差小于或等于K。

          无数惨痛教训证明用双循环肯定是找死,一定会超时。

           把nums中的每个element转变为dict的key,index转化为dict的value。

           此题要注意这样的例子:[1,0,2,1,1,4] ,k=1,要排除掉array转dict时,数值相同,但是两个数的index相差大于k的情况。

    leetcode219[easy]----Contains Duplicate II


相关文章:

  • 2022-01-19
  • 2021-07-31
  • 2021-06-16
  • 2021-09-04
  • 2021-06-16
猜你喜欢
  • 2022-01-14
  • 2022-12-23
  • 2021-09-21
  • 2021-07-09
  • 2021-09-02
  • 2021-08-19
  • 2021-08-04
相关资源
相似解决方案