Saturday, August 29, 2015

Leetcode 219. Contains Duplicate II

https://leetcode.com/problems/contains-duplicate-ii/

Solution:
# T:O(n) S:O(n)
class Solution:
    # @param {integer[]} nums
    # @param {integer} k
    # @return {boolean}
    def containsNearbyDuplicate(self, nums, k):
        if k >= len(nums) - 1:
            return len(nums) > len(set(nums))
        for i in xrange(k, len(nums)):
            tmp = nums[i-k:i+1]
            if len(tmp) > len(set(tmp)):
                return True
        else:
            return False
Run Time: 60 ms

No comments:

Post a Comment