·思路:
利用位运算对题目进行解答,由题知需要找出给出的数组nums中只出现一次的数组,则根据位运算中^=的性质运算:两个数对应位相同时返回0,否则为1,可进行遍历尝试。
from typing import List
class Solution:
def singlenumber(self,nums:List[int]) ->List[int]:
n=0 #初始化变量n
for num in nums: #设置循环将遍历数组中产生的num
n^=num#利用异或运算判断n是否等于遍历中的num数,相同时返回num=x
return n
s=Solution()
print(s.singlenumber([2,2,7]))