-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path2429-minimize-xor.py
41 lines (31 loc) · 1.06 KB
/
2429-minimize-xor.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
37
38
39
40
41
# time complexity: O(logn)
# space complexity: O(1)
class Solution:
def minimizeXor(self, num1: int, num2: int) -> int:
result = num1
targetSetBitsCount = bin(num2).count("1")
setBitsCount = bin(result).count("1")
currentBit = 0
while setBitsCount < targetSetBitsCount:
if not self.isSet(result, currentBit):
result = self.setBit(result, currentBit)
setBitsCount += 1
currentBit += 1
while setBitsCount > targetSetBitsCount:
if self.isSet(result, currentBit):
result = self.unsetBit(result, currentBit)
setBitsCount -= 1
currentBit += 1
return result
def isSet(self, x: int, bit: int) -> bool:
return (x & (1 << bit)) != 0
def setBit(self, x: int, bit: int):
return x | (1 << bit)
def unsetBit(self, x: int, bit: int):
return x & ~(1 << bit)
num1 = 3
num2 = 5
print(Solution().minimizeXor(num1, num2))
num1 = 1
num2 = 12
print(Solution().minimizeXor(num1, num2))