书里面关于分类的推断有些麻烦,通过某一位为0为1来对数组元素进行分类。假如第3位为1。那么也就是元素x & 8 等于或不等于0,所以不是必需非的用第几位去推断。
def once(array): reOR = 0 for x in array: reOR ^= x bit1 = firstBit1(reOR) first = 0 second = 0 for x in array: if x & bit1 != 0: first ^= x else: second ^= x return first, second def firstBit1(x): n = 1 while True: if x & n != 0: return n else: n <<= 1