forked from DmrfCoder/AlgorithmAndDataStructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path40.py
36 lines (30 loc) · 847 Bytes
/
40.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- coding:utf-8 -*-
class Solution:
# 返回[a,b] 其中ab是出现一次的两个数字
def FindNumsAppearOnce(self, array):
# write code here
if array is None:
return []
xorResult = array[0]
for i in range(1, len(array)):
xorResult = xorResult ^ array[i]
if xorResult == 0:
return []
index = 0
while (xorResult & 1) == 0:
xorResult=xorResult >> 1
index += 1
a = b = 0
for itemArray in array:
if self.isBit(itemArray, index):
a = a ^ itemArray
else:
b = b ^ itemArray
return [a, b]
def isBit(self, data, index):
data=data >> index
return data & 1
a=[1,1,2,2,3,4,4,5]
s=Solution()
b=s.FindNumsAppearOnce(a)
print b