56 数组中数字出现的次数

一个数组除了2个数字之外,所有数字都出现2次,找出出现1次的两个数字

class Solution():
    def onlynum(self,A):
        res = 0
        for i in A:
            res ^= i
        resString = str(bin(res)[2:])
        index1 = len(resString)-1
        for s in resString:
            if s == 0:
                index1 -=1
            else:
                break

        groupA = []
        groupB = []

        for j in A:
            if str(bin(j)[2:])[::-1][index1] == '1':
                groupA.append(j)
            else:
                groupB.append(j)


        
        numA = 0
        for i in groupA:
            numA ^= i
        numB = 0
        for i in groupB:
            numB^= i
        return numA,numB




s = Solution()
A = [2,4,3,6,3,2,5,5]
print(s.onlynum(A))

你可能感兴趣的:(56 数组中数字出现的次数)