Why my code doesn't work


  • 0
    B

    class Solution(object):
    def containsNearbyDuplicate(self, nums, k):
    """
    :type nums: List[int]
    :type k: int
    :rtype: bool
    """
    if nums == [] or len(nums) == 1:
    return False;

        value_indexlist_dict = {};
        for i in range(0, len(nums)):
            if value_indexlist_dict.get(nums[i], -1) == -1:
                indexlist = [];
                indexlist.append(i);
                value_indexlist_dict[nums[i]] = indexlist;
            else:
                indexlist = value_indexlist_dict[nums[i]];
                indexlist.append(i);
                value_indexlist_dict[nums[i]] = indexlist;
                
        for k, v in value_indexlist_dict.items():
            if len(v) == 2 and abs(v[0] - v[1]) <= k:
                    return True;
                
        return False;
    

    it seems that leetcode doesn't support abs operation in python ?


Log in to reply
 

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