python leetcode 日记 --Contains Duplicate II --219
题目:
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的更多相关文章
- python leetcode 日记 --Contains Duplicate --217
题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...
- leetcode日记 HouseRobber I II
House Robber I You are a professional robber planning to rob houses along a street. Each house has a ...
- 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 ...
- python leetcode 日记--231. Power of Two
题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): ...
- LeetCode(43)-Contains Duplicate II
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- leetCode(28):Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i ...
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- [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 ...
- 219. Contains Duplicate II - LeetCode
Question 219. Contains Duplicate II Solution 题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false 思路:用一个map记录每 ...
随机推荐
- PHP获取毫秒时间戳,利用microtime()函数
PHP获取毫秒时间戳,利用microtime()函数 php本身没有提供返回毫秒数的函数,但提供了一个microtime()函数,借助此函数,可以很容易定义一个返回毫秒数的函数.php的毫秒是没有默认 ...
- windows+caffe(一)——自己环境
环境:win7 旗舰版升级到sp1 虚拟机 无GPU vs2013 matlab2016a python2.7 安装caffe已经成功 安装过程见我的另一篇:http://www.cnblogs.co ...
- 2x2矩阵相乘模版
由于Unity只有4x4矩阵,今天要做一个2x2矩阵的旋转,居然忘了顺序.故写下作为模版记录. 顺序: 下面是使用其进行旋转的C#代码: public struct Position { public ...
- 制作圆角:《CSS3 Border-radius》
今天我们在一起来看看CSS3中制作圆角的属性border-radius的具体用法.如今CSS3中的border-radius出现后,让我们没有那么多的烦恼了,首先制作圆角图片的时间是省了,而且其还有多 ...
- VS2013: upgrading a Windows Phone 7/8 and Windows 8 apps(转)
VS2013: upgrading a Windows Phone 7/8 and Windows 8 apps September 17, 2013Windows 8, Windows PhoneJ ...
- Maven学习(一) -- 安装Maven及Eclipse中配置Maven
标签(空格分隔): 学习笔记 本文环境:Windows7, JDK1.7.0_76 安装及配置Maven环境变量 需要电脑中已经有Java环境 在控制台中输入:echo %JAVA_HOME%看是否能 ...
- 【树莓派】树莓派移动网络连接(配置4G网卡)
1.识别上网卡 使用命令lsusb,如果能够识别则可以继续往下. 2.安装网卡驱动程序 上网卡一般都会带有2个功能,一个是虚拟光驱,用来安装驱动程序:另一个是进行网络连接的modem. 在Linux下 ...
- 06-scanf函数
本文目录 变量的内存分析 scanf函数 回到顶部 一. 变量的内存分析 1. 字节和地址 为了更好地理解变量在内存中的存储细节,先来认识一下内存中的“字节”和“地址”. 1> 内存以“字节为单 ...
- js函数、变量提升(hoisting)
其实我只是想复习下变量提升的,然后看到了函数提升,然后再看到了函数声明.函数表达式. 有必要怀着敬仰之心提及园子里的TOM大叔的解密命名函数表达式,不愧是大叔,好好地脑补了下基础知识. 在ECMASc ...
- sqlAlchemy 按DateTime字段的年或月进行group_by查询
一.根据”create_date“查询每天的数据 1.查询2016年5月每天的数据 session.query(extract('day', User.create_date).label('day' ...