题目链接:https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof/
难度:中等

描述:
在一个数组 nums 中除一个数字只出现一次之外,其他数字都出现了三次。请找出那个只出现一次的数字。

题解

  1. class Solution:
  2. def singleNumber(self, nums: List[int]) -> int:
  3. ones, twos = 0, 0
  4. for num in nums:
  5. ones = ones ^ num & ~twos
  6. twos = twos ^ num & ~ones
  7. return ones