[easy][Array][HashTable]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.

思路是:

这一题的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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容