12 lines
373 B
Python
12 lines
373 B
Python
# https://leetcode.com/problems/number-complement
|
|
class Solution:
|
|
def findComplement(self, num: int) -> int:
|
|
def bit_length(num):
|
|
if num == 0:
|
|
return 1
|
|
return len(bin(num)) - 2
|
|
width = bit_length(num)
|
|
inverted = ~num
|
|
mask = (1 << width) - 1
|
|
result = inverted & mask
|
|
return result
|