题目:

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。

        """
:type nums: List[int]
:type k: int
:rtype: bool
"""

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

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

class Solution(object):
def containsNearbyDuplicate(self, nums, k):
if k==0:
return False
while len(nums)>1:
tem=nums.pop(0)#没那一个数出来比较,原列表少一,减少搜索空间,但减少的很慢,效率很低
j=self.findLocation(nums,tem,k)
if j<k and j>=0:
return True
return False
def findLocation(self,L,number,k):
for item in L:
if item ==number:
return L.index(item)
k-=1
if k==0:
return -1
return -1

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

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

class Solution(object):
def containsNearbyDuplicate(self, nums, k):
dictionary={}
for key,value in enumerate(nums):
if value in dictionary and key-dictionary[value]<=k:
return True
dictionary[value]=key
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. static const vs. extern const

    在实现文件(.m文件)中使用static const来定义“只在编译单元内可见的常量”(只在.m文件内可见),由于此类常量不在全局符号表中,所以无须为其名称加类名前缀(一般以k开头). 在头文件中使用 ...

  2. Oracle 流复制实践笔记

    最近因为业务需求,需要在两个数据库之间做双向实时同步,遂实践了一把Oracle的流复制,遇到了很多疑难问题,最终也貌似成功,现记录如下. 我是使用OEM来实现流复制的. 10.进行流复制的两个数据库的 ...

  3. RCP:如何移除Search对话框中不需要的项

    前言 很久没写文章了,准备写一系列关于Eclipse RCP /Plugin的文章. 这些文章都是trouble shooting性质的,不准备写的很细,当你碰到这样的问题,google到时,能帮你把 ...

  4. 关于token的杂记

    http://www.cnblogs.com/xiekeli/p/5607107.html https://www.oschina.net/question/1264088_220768 token作 ...

  5. INPUT只能输入数字

    input只能输入数字: (只能输入数字,并且输入的值不能大于99),但是这样有个问题,就是当输入非数字字符时,输入框中所有的字符都会被清除 <input type="text&quo ...

  6. iOS开发 百度坐标点的计算

    BMKMapPoint point1 = BMKMapPointForCoordinate(_userGps); BMKMapPoint point2 = BMKMapPointForCoordina ...

  7. Security » Authorization » 基于声明的授权

    Claims-Based Authorization¶ 基于声明的授权 142 of 162 people found this helpful When an identity is created ...

  8. JS判断数组中是否有重复元素的方法

    判断数组中是否有重复元素,最容易想到的方法是使用2重循环,逐个遍历,比较,但是这个是最慢,最笨的方法,百度得出了更好的方法. var ary = new Array("111",& ...

  9. JAVA学习之路(环境配置,)

    最近过去的寒假我就开始看一些JAVA的学习视频了,视频是毕向东老师的,讲得还不错,东北口音,欧了没? 首先是一些基础概念. 1.JAVA的三种技术架构 企业版 J2EE 标准版 J2SE 小型版 J2 ...

  10. [maven] 常用插件解析

    参考资料:http://my.oschina.net/zh119893/blog/276090 我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完 ...