这道题为简单题
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.
1、我是用的字典,如果已经在字典键中,就返回True,否则就把该值和引索值添加到字典中
2、我看见有个大神代码更简单,直接比较现有的长度和set(该列表)的长度,如果一样就返回True,否则False
我的:
1 class Solution(object): 2 def containsDuplicate(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: bool 6 """ 7 a = {} 8 for i in nums: 9 if i in a: return True 10 a[i] = i 11 return False
大神:
1 class Solution(object): 2 def containsDuplicate(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: bool 6 """ 7 return len(nums) != len(set(nums))
原文:http://www.cnblogs.com/liuxinzhi/p/7544607.html