-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2527-find-xor-beauty-of-array.py
More file actions
48 lines (37 loc) · 1.07 KB
/
2527-find-xor-beauty-of-array.py
File metadata and controls
48 lines (37 loc) · 1.07 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
from typing import List
class Solution:
def xorBeauty(self, nums: List[int]) -> int:
binaryList = [0] * 32
for num in nums:
for i, bint in enumerate(reversed(bin(num)[2:])):
binaryList[i] += int(bint)
binaryList = [str(num % 2) for num in binaryList]
return int("".join(binaryList[::-1]), 2)
'''
- (0,0,0) with effective value ((1 | 1) & 1) = 1
- (0,0,1) with effective value ((1 | 1) & 4) = 0
- (0,1,0) with effective value ((1 | 4) & 1) = 1
- (0,1,1) with effective value ((1 | 4) & 4) = 4
- (1,0,0) with effective value ((4 | 1) & 1) = 1
- (1,0,1) with effective value ((4 | 1) & 4) = 4
- (1,1,0) with effective value ((4 | 4) & 1) = 0
- (1,1,1) with effective value ((4 | 4) & 4) = 4
1 = 0001
4 = 0100
(0001 | 0001) & 0001 = 0001 -> (i, i, i) = num[i]
15 = 001111
45 = 101101
20 = 010100
02 = 000010
34 = 100010
35 = 100011
05 = 000101
44 = 101100
32 = 100000
30 = 011110
34 = 100010
'''
nums = [1, 4]
print(Solution().xorBeauty(nums))
nums = [15, 45, 20, 2, 34, 35, 5, 44, 32, 30]
print(Solution().xorBeauty(nums))