美文网首页
Leetcode-217题:Contains Duplicate

Leetcode-217题:Contains Duplicate

作者: 八刀一闪 | 来源:发表于2016-09-26 22:23 被阅读14次

    题目

    Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

    代码

    class Solution(object):
        def containsDuplicate(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            if nums==None or len(nums)==0:
                return False
            unique_nums = set()
            for n in nums:
                if n in unique_nums:
                    return True
                else:
                    unique_nums.add(n)
            return False
    

    相关文章

      网友评论

          本文标题:Leetcode-217题:Contains Duplicate

          本文链接:https://www.haomeiwen.com/subject/uoaqyttx.html