• 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
            
  • 相关阅读:
    Luogu-P1404 平均数
    树的直径与重心
    卡常技巧
    背包问题总结
    Codevs-1521 华丽的吊灯
    区间dp与环形dp
    Luogu-P1308 神经网络
    拓扑排序
    01分数规划
    Python学习 4day__基础知识
  • 原文地址:https://www.cnblogs.com/bonelee/p/8685896.html
Copyright © 2020-2023  润新知