20201020-只出现一次的数|异或运算

it2023-11-01  74

136.只出现一次的数

class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ a = 0 for i in nums: a ^= i return a 用异或做因为题目要求不能使用额外空间另有用set做的,但是占用了额外空间 class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ return sum(set(nums)*2)-sum(nums)
最新回复(0)