原題是:
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的给涕。但是因?yàn)檫@個(gè)題十分簡(jiǎn)單融师,用排序也是可以的。
代碼采用了排序方式。
代碼是:
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