美文网首页
[easy][Array][HashTable]217. Con

[easy][Array][HashTable]217. Con

作者: 小双2510 | 来源:发表于2017-11-24 13:36 被阅读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.

    思路是:

    这一题的tag是有hash table的。但是因为这个题十分简单,用排序也是可以的。
    代码采用了排序方式。

    代码是:

    class Solution:
        def containsDuplicate(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            if not nums:
                return False
            sort = sorted(nums)
            for i in range(len(sort)):
                if i+1 < len(sort) and sort[i] == sort[i+1]:
                    return True
            return False
    

    相关文章

      网友评论

          本文标题:[easy][Array][HashTable]217. Con

          本文链接:https://www.haomeiwen.com/subject/idaxbxtx.html