题目:

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 difference between i and jis at most k.

给定一个整形数组和一个整数型数k,找出在这个数组中是否存在两个相同的数,并且这两个数的下标的距离小于k。

  1. """
  2. :type nums: List[int]
  3. :type k: int
  4. :rtype: bool
  5. """

根据题目可知输入为一个list和一个整形,返回值为bool

因为此题不能改变数组,因此最原始的一种办法就是遍历:

  1. class Solution(object):
  2. def containsNearbyDuplicate(self, nums, k):
  3. if k==0:
  4. return False
  5. while len(nums)>1:
  6. tem=nums.pop(0)#没那一个数出来比较,原列表少一,减少搜索空间,但减少的很慢,效率很低
  7. j=self.findLocation(nums,tem,k)
  8. if j<k and j>=0:
  9. return True
  10. return False
  11. def findLocation(self,L,number,k):
  12. for item in L:
  13. if item ==number:
  14. return L.index(item)
  15. k-=1
  16. if k==0:
  17. return -1
  18. return -1

但是在提交时发现此种方法无法通过例子list(range(30000)) 15000这个测试,分析发现因为如果数组的数全不一样,那么其复杂度为O(n2)。

在查找解决方法后,学习了python中字典的使用,参考https://leetcode.com/discuss/54123/python-concise-solution-with-dictionary

  1. class Solution(object):
  2. def containsNearbyDuplicate(self, nums, k):
  3. dictionary={}
  4. for key,value in enumerate(nums):
  5. if value in dictionary and key-dictionary[value]<=k:
  6. return True
  7. dictionary[value]=key
  8. return False

通过使用字典,将算法复杂性变为O(n)

如果使用c++或JAVA,本题则需使用hashtable进行相似的处理。

python leetcode 日记 --Contains Duplicate II --219的更多相关文章

  1. python leetcode 日记 --Contains Duplicate --217

    题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...

  2. leetcode日记 HouseRobber I II

    House Robber I You are a professional robber planning to rob houses along a street. Each house has a ...

  3. python leetcode 日记--Maximal Square--221

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  4. python leetcode 日记--231. Power of Two

    题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): ...

  5. LeetCode(43)-Contains Duplicate II

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  6. leetCode(28):Contains Duplicate II

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

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

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

  8. [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 ...

  9. 219. Contains Duplicate II - LeetCode

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

随机推荐

  1. Openstack命令行创建不同vlan段虚拟机

    默认使用nova-network的vlan模式,但是在使用默认的dashboard的时候,不能指定创建的虚拟机的使用网段,固定IP地址. 实际上该功能是在存在的,只是openstack的dashbbo ...

  2. MVC过滤器使用案例:统一处理异常顺道精简代码

    重构的乐趣在于精简代码,模块化设计,解耦功能……而对异常处理的重构则刚好满足上述三个方面,下面是我的一点小心得. 一.相关的学习 在文章<精简自己20%的代码>中,讨论了异常的统一处理,并 ...

  3. WPF:常见问题

    1.自定义Main函数 背景: wpf 默认的Main函数在 App.g.cs文件中,在App.xmal.cs内自定义Main函数后冲突. 解决方法: 法一: 1)新建class1.cs类,在其中设置 ...

  4. 基于Opencv和Mfc的图像处理增强库GOCVHelper(索引)

    GOCVHelper(GreenOpen Computer Version Helper )是我在这几年编写图像处理程序的过程中积累下来的函数库.主要是对Opencv的适当扩展和在实现Mfc程序时候的 ...

  5. 2016年12月24日 星期六 --出埃及记 Exodus 21:19

    2016年12月24日 星期六 --出埃及记 Exodus 21:19 the one who struck the blow will not be held responsible if the ...

  6. [问题2014A05] 解答

    [问题2014A05]  解答 (1) 将矩阵 \(A\) 分解为两个矩阵的乘积: \[A=\begin{bmatrix} 1 & 1 & \cdots & 1 & 1 ...

  7. Unity-Animator深入系列---API详解

    回到 Animator深入系列总目录 测试Unity版本为5.2.1 人形动画的接口都有标注 本列表不包含所有标注为过时的方法 1.Vector3 angularVelocity { get; } [ ...

  8. SpringBoot集成jsp(附源码)+遇到的坑

    1.大体步骤 (1)       创建Maven web project: (2)       在pom.xml文件添加依赖: (3)       配置application.properties支持 ...

  9. 《BI项目笔记》数据源视图设置

    目的数据源视图是物理源数据库和分析维度与多维数据集之间的逻辑数据模型.在创建数据源视图时,需要在源数据库中指定包含创建维度和多维数据集所需要的数据表格和视图.BIDS与数据库连接,读取表格和视图定义, ...

  10. Linux变量

    变量:(大的分为环境变量与本的变量) 本地变量: 本地变量在用户现在的shell生命期的脚本中使用.例如,本地变量file-name="loop.doc",这个值只在用户当前she ...