Yingying

你必须非常努力,才能看起来毫不费力

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.

My code:

class Solution(object):
    def containsDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        n = len(nums)
        if len(nums)<2:
           return False
        nums.sort()
        for i in range(1,n):
            if nums[i]== nums[i-1]:
                return True
        return False

If the dictionary is used to find the duplicates, there would be a lot of searching about the keys. So the list should be sorted first to save time.
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/alwaystry/article/details/52346603
个人分类: leetcode
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

217. Contains Duplicate *

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭