Why my python code wrong?


  • 0
    N
    __author__ = 'Neal'
    from bisect import bisect_left
    class Solution:
        # @param {integer[]} nums
        # @param {integer} k
        # @param {integer} t
        # @return {boolean}
        def containsNearbyAlmostDuplicate(self, nums, k, t):
            nums_dict = []
            for positon in range(0, len(nums)):
                if len(nums_dict) == k+1:
                    nums_dict.remove(nums[positon-k-1])
                nums_dict.sort()
                temp = bisect_left(nums_dict, nums[positon]-t)
                if temp != len(nums_dict) and nums_dict[temp]-nums[positon] <= t:
                    return True
                nums_dict.append(nums[positon])
            return False
    
    solu =Solution()
    print(solu.containsNearbyAlmostDuplicate([], 0, 0))
    print(solu.containsNearbyAlmostDuplicate([-1, -1], 1, 0))
    print(solu.containsNearbyAlmostDuplicate([7, 1, 3], 2, 3))

Log in to reply
 

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