原题是:
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