力扣https://leetcode-cn.com/problems/single-element-in-a-sorted-array/异或的原理的,相同的数异或为0、异或在python中的两种写法如下:
from functools import reduceimport operatorclass Solution: def singNonDuplicate(self,nums): return reduce(lambda x,y:x^y,nums) def singNonDuplicate(self,nums): return reduce(operator.xor,nums)if __name__ == "__main__": nums = [1,1,2,3,3,4,4,8,8] print(Solution().singNonDuplicate(nums))