Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Subscribe to see which companies asked this question
思路
位操作
0^a=a
a^b^c^a^c=b
class Solution(object):
def singleNumber(self,nums):
nums.sort()
ans=0
if not len(nums):
return 0
for i in range(len(nums)):
ans=ans^nums[i]
return ans