zoukankan      html  css  js  c++  java
  • leetcode 217. Contains Duplicate

    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
            """
            s = set()
            for n in nums:
                if n in s:
                    return True
                else:
                    s.add(n)
            return False
            

    or return len(nums) != len(set(nums))

    class Solution(object):
        def containsDuplicate(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            return len(collections.Counter(nums)) != len(nums)
            

    or

    class Solution(object):
        def containsDuplicate(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            nums.sort()
            for i in xrange(1, len(nums)):
                if nums[i] == nums[i-1]:
                    return True
            return False
            
  • 相关阅读:
    hanoi(老汉诺塔问题新思维)
    ABP文档
    ABP文档
    ABP文档
    ABP文档
    ABP文档
    ABP文档
    ABP文档
    ABP文档
    ABP框架
  • 原文地址:https://www.cnblogs.com/bonelee/p/8685896.html
Copyright © 2011-2022 走看看