书里面关于分类的判断有些麻烦,通过某一位为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
【剑指offer】Q40:数组中出现一次的数,布布扣,bubuko.com
原文:http://blog.csdn.net/shiquxinkong/article/details/36522321