問題描述:
給定一個整數(shù)數(shù)組赏枚,判斷其中是否包含重復(fù)元素。你要寫的函數(shù)饿幅,當包含重復(fù)元素時返回 true,否則返回 false
代碼示例一:利用python中的字典
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
n = len(nums)
if n <= 0 : return False
dd = {}
for index, value in enumerate(nums):
if value in dd:
return True
else:
dd[value] = 1
return False
代碼示例二:利用python中的set去重函數(shù)(效果比一好很多)
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
nums_copy = set(nums)
n1 = len(nums)
n2 = len(nums_copy)
if n1 == n2 : return False
else: return True