只出现一次出现的数字

只出现一次出现的数字_第1张图片
自己的思路(超时):
用两层循环去比较列表中的数字是否相等,若相等则赋值为空字符,最后用一层for循环输出不为空的字符,复杂度为o(3n)

# class Solution:
#     def singleNumber(self, nums):
#
#         for i in range(len(nums)):
#             for j in range(i+1,len(nums)):
#                 if nums[i] ==nums[j]:
#
#                     nums[j] =''
#                     nums[i] =''
#         for i in nums:
#             if i !='':
#                 return i

大佬的解法:
使用异或,任意数与0 异或值不变,相同数异或值为0

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
    
        a = 0
        for i in nums:
            a = a^i
        return a

你可能感兴趣的:(只出现一次出现的数字)