关闭

217. Contains Duplicate *

67人阅读 评论(0) 收藏 举报
分类:

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.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:55495次
    • 积分:3857
    • 等级:
    • 排名:第8568名
    • 原创:332篇
    • 转载:90篇
    • 译文:0篇
    • 评论:11条
    最新评论