[LeetCode] Single Number II

问题描述:

  Given an array of integers, every element appears three times except for one. Find that single one. 

参考出现两次的,也是需要异或运算,问题是现在得判断出现三次归零的运算。需要两个辅助变量,参考了别人的代码。

class Solution:
    # @param A, a list of integer
    # @return an integer
    def singleNumber(self, A):
        ones,twos=0,0
        for ele in A:
            ones = ones^ele & ~twos
            twos = twos^ele & ~ones
        return ones

你可能感兴趣的:(LeetCode)