题目python
方法一:spa
class Solution: def containsDuplicate(self, nums): """ :type nums: List[int] :rtype: bool """ d = dict() for i in nums: if i in d: return True d[i] = i return False
思路:code
遍历整个list,若是发现list中的元素已经在字典中存在,说明有重复的元素,若是没在字典中,在遍历过程当中将元素添加到字典中,直到整个list末尾。blog
方法二:ip
class Solution: def containsDuplicate(self, nums): """ :type nums: List[int] :rtype: bool """ S = set(nums) if len(S) < len(nums): return True else: return False
思路:leetcode
使用集合的特性,集合中不会包含重复元素。get