【leetcode】217. 存在重复元素(python)

【leetcode】217. 存在重复元素(python)_第1张图片

class Solution(object):
    def containsDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        # 方法一:暴力求解, 超出时间限制, error!
        # n = len(nums)
        # for i in range(n):
        #     for j in range(i+1, n):
        #         if nums[i] == nums[j]:
        #             return True
        # return False

        # 方法二:排序,然后一次遍历即可。
        # nums = sorted(nums) # AC, but it takes 108ms, too long...
        # n = len(nums)
        # for i in range(n - 1):
        #     if nums[i] == nums[i + 1]:
        #         return True
        # return False

        # 方法三:用set()
        return len(set(nums)) != len(nums) # AC, it takes 40ms

你可能感兴趣的:(Python,Leetcode,python,数组,哈希表)