Use enumerate for simpler code

  • 0

    you can use enumerate to do the same with much simpler code

    class Solution:
        # @param {integer[]} nums
        # @param {integer} k
        # @return {boolean}
        def containsNearbyDuplicate(self, nums, k):
            sequence = list(enumerate(nums))
            sequence.sort(key = lambda x: x[1])
            for i in xrange(len(sequence) - 1):
                if (sequence[i][1] == sequence[i+1][1]) and (abs(sequence[i][0] - sequence[i+1][0]) <= k):
                    return True
            return False

Log in to reply

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