Help ! my python code works in my computer, but it's wrong anwser in submit code,why?


  • 0
    H
    class Solution:
        # @param {integer[]} nums
        # @param {integer} k
        # @return {boolean}
        def containsNearbyDuplicate(self, nums, k):
                d=dict()
                if(len(nums)<=1 or k==0):
                    return False
                for r in range(0,len(nums)):
                    d[str(r)]=nums[r]
                dd=sorted(d.items(),key=lambda d:int(d[1]))
                a=[]
                b=[]
                for k,v in dd:
                    a.append(int(k))
                    b.append(int(v))
                for i in range(len(nums)-1):
                    if(b[i]==b[i+1]):
                        if (abs(a[i]-a[i+1])<=int(k)):
                            return True
                return False

  • 0
    D

    k is a paramter of the method

    def containsNearbyDuplicate(self, nums, ***k***)
    

    But you use a local variable k and it's overwritten

        for k,v in dd:
            a.append(int(k))
            b.append(int(v))
    

  • 0
    H

    Thank you,I don't notice that!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.