题目要求

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.

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

题目分析及思路

给出一个整数数组,判断这个数组是否包含重复值。若有任意值出现两次及以上则返回True,若每个值都只出现一次,则返回False。可以使用集合的特性获取数组中不同字符的个数,若和原数组的长度相等,则返回False。否则则返回True。

python代码

class Solution:

    def containsDuplicate(self, nums: List[int]) -> bool:

        nums_s = set(nums)

        if len(nums) == len(nums_s):

            return False

        else:

            return True

        

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄