赞
踩
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
XOR. (Sort the array is more faster?)
class Solution:
def singleNumber(self, nums: List[int]) -> int:
ans = 0
sLen = len(nums)
for i in range(sLen):
ans ^= nums[i]
return ans
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。