LeetCode-136-只出现一次的数字

2020-12-04  本文已影响0人  阿凯被注册了
image.png

解题思路:

  1. 按位异或,同一个数字出现两次,表示该位上异或两次,该位的值还是0;
  2. 出现一次的值即是异或的结果。

Python3代码:

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        s = 0
        for num in nums:
            s^=num
        return s
上一篇 下一篇

猜你喜欢

热点阅读