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记录每 ...
随机推荐
- 你不要用战术上的勤奋掩盖战略上的懒惰by雷军
这个问题很有趣. 战略.战术.勤奋.懒惰,我们拆开来看吧,虽然我觉得其实分析一个人说话是为什么是很没有意义的事情.我们要先了解一下,公司的组织中,谁是指定战略的,谁是指定战术的.公司战略和战术意味着什 ...
- printk函数日志级别的设置【转】
本文转载自: 下面执行cat /proc/sys/kernel/printk 打印出的四个数字分别代表: 控制台日志级别.默认的消息日志级别.最低的控制台日志级别和默认的控制台日志级别 只有当prin ...
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
在更新项目之后,做了一定的改动后发现竟然报错了,刚才还好好的. java.sql.SQLException: Access denied for user 'root'@'localhost' (us ...
- ipad开发:二维码扫描,摄像头旋转角度问题解决办法
之前一直是在手机上开发,用系统原生二维码扫描功能,一点问题都没有,但是在ipad上,用户是横屏操作的,虽然界面旋转了,是横屏的,但是摄像头里显示的依然是竖屏效果,也就是说从摄像头里看到的和人眼看到的内 ...
- HDU Machine scheduling
Machine scheduling Time Limit : 5000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- 系统巡警 v1.2 系统行为分析神器
系统巡警,是一款安全辅助软件,可以帮助系统维护人员或安全研究人员观察系统运行情况,包括进程启动与销毁记录.模块加载记录.线程启动与销毁记录.系统服务创建修改和删除记录.文件与文件夹的增加删除和修改记录 ...
- spring+mybatis+druid+mysql+maven事务配置
1.首先pom.xml文件里面需要用到的jar配置: <!-- spring事务,包含了@Transactional标注 --> <dependency> <groupI ...
- [问题2014A11] 解答
[问题2014A11] 解答 我们需要利用以下关于幂等阵判定的结论,它是复旦高代书第 142 页的例 3.6.4: 结论 设 \(A\) 为 \(n\) 阶方阵, 则 \(A^2=A\) 当且仅当 ...
- MYSQL转换JSON
http://51strive.com/ <!DOCTYPE html><html><head><meta charset="UTF-8" ...
- parseInt的源码阅读
parseInt的源码阅读 Integer.parseInt()这个方法的功能小巧又实用,实现起来困难不大,没有很复杂.这里就来看一下Java的源码是怎么写的吧,走一边大婶写过的代码,应该会有点收获吧 ...