Skip to content

Commit 145834d

Browse files
committed
add 56-i數組出現的次數.py
1 parent 1cfc7e9 commit 145834d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

medium/.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 全部xor找到第一個1,做分組,0的1組,1的一組
2+
3+
class Solution:
4+
def singleNumbers(self, nums):
5+
x = 0
6+
for i in nums:
7+
x ^= i
8+
start = 0
9+
while x:
10+
if not (x & 1):
11+
start += 1
12+
x >>= 1
13+
else:
14+
break
15+
a = 0
16+
b = 0
17+
for i in nums:
18+
if (i >> start) & 1:
19+
a ^= i
20+
else:
21+
b ^= i
22+
return [a, b]
23+
'''
24+
if __name__ == '__main__':
25+
sol = Solution()
26+
nums = [1, 2, 5, 2]
27+
print (sol.singleNumbers(nums))
28+
'''

0 commit comments

Comments
 (0)