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进行相似的处理。

时间: 2024-12-23 23:59:33

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

LeetCode:Contains Duplicate II - 判断数组内是否有重复元素2

1.题目名称 Contains Duplicate II(判断数组内是否有重复元素2) 2.题目地址 https://leetcode.com/problems/contains-duplicate-ii/ 3.题目内容 英文: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] = nu

[LeetCode][JavaScript]Contains Duplicate II

Contains Duplicate II Given an array of integers and an integer k, return true if and only if there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. https://leetcode.com/proble

【LeetCode】Contains Duplicate II

Contains Duplicate II 问题描述 Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. 算法思想 思想一: 两个数组下标i,j,在叫j-i<

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 return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 本题为一个典型的动态规划问题,因此可以使用动态规划的思想进行. step1,初始化

python leetcode 日记--231. Power of Two

题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): def isPowerOfTwo(self, n): # """ :type n: int :rtype: bool """ 方法:分析2的幂次方的特点,发现2的任意次方的数,化成二进制时只有首位为1其余位为0,因此我的解决方法如下: class

【LeetCode】Contains Duplicate II 解题小结

题目: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 j is at most k. 题目意思就是判断在一个数组中是否存在两个相同的元素,他们之间的距离在k以内. 自己开始的思路是嵌套的两个for循

leetCode(28):Contains Duplicate II

Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k. 哈希表的使用 class Solution { public: bool containsNearbyDup

leetCode 219. Contains Duplicate II 数组

219. Contains Duplicate II 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 j is at most k. 题目大意: 找到数组中两个相同元素,如果这两个元素的距离小于等于k

LeetCode | 面试题59 - II. 队列的最大值【Python】

LeetCode 面试题59 - II. 队列的最大值[Medium][Python][队列] 问题 力扣 请定义一个队列并实现函数 max_value 得到队列里的最大值,要求函数max_value.push_back 和 pop_front 的均摊时间复杂度都是O(1). 若队列为空,pop_front 和 max_value 需要返回 -1 示例 1: 输入: ["MaxQueue","push_back","push_back",&quo