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.
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
思路:
异或交换原理:
- 数字A异或B两次,就得到A。而B被A异或两次,就得到B
任意数与自己异或操作都是0,任意数与0异或操作都得到本身。 【这个思路完全错误,因为需要要求数组中单个的元素只有一个!】
- Python中set会移走重复的元素,我们可以比较nums的长度和set(nums)的长度
def containsDuplicate2(self, nums):
return len(nums) > len(set(nums))