题目:

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. InnerJoin分页导致的数据重复问题排查

    2016年8月9号美好的七夕的早上,我精神抖擞地来到公司.一会之后,客服宅宅MM微信我,说一个VIP大店铺订单导出报表中一个订单有重复行.于是,我赶紧开始查探问题所在.经过一天的反复仔细追查(当然还包 ...

  2. [已解决]Eclipse 插件Maven在使用 add dependency,找不到包,解决办法

    以Eclipse版本[Version: Luna Release (4.4.0),]为例, 依次打开:Window >show view > other > Maven Reposi ...

  3. 微信OAuth2.0网页受权php

    www.MyException.Cn 网友分享于:2014-01-19 浏览:2504次 微信OAuth2.0网页授权php示例 1.配置授权回调页面域名,如 www.aaa.com 2.模拟公众号的 ...

  4. 禁止输入中文 与 禁止输入数字在phonegap api环境效果

    例子如下: <!doctype html> <html> <head> <meta charset="utf-8"> <tit ...

  5. linux 循环处理文件夹下所有文件脚本

    #!/bin/bashfunction ergodic(){ for file in ` ls $1 ` do if [ -d $1"/"$file ] then ergodic ...

  6. Visual Studio的Web Performance Test提取规则详解(1)

    总结 Visual Studio的Web Performance Test是基于HTTP协议层的,它不依赖于浏览器,通过直接接收,发送HTTP包来和Web服务器交互.Web Performance T ...

  7. Hadoop安装指引

    pre.ctl { font-family: "Liberation Mono", monospace } p { margin-bottom: 0.25cm; line-heig ...

  8. java解析json与map,list相互之间的转换

    运行这个类需要加载jar包:ezmorph-1.0.6.jar.json-lib-2.4-jdk15.jar.jsoup-1.6.1.jar.commons-beanutils-1.8.0.jar.c ...

  9. uploadify springMVC

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  10. lua中string.find()函数作用于汉字字符串

    lua中有这样一个库函数,string,find(),作用是在一个字符串中找到目标字符串的起始和结束位置(从1开始计数) 如:a,b=string.find("hello world&quo ...