This commit is contained in:
parent
695d4ac9f5
commit
ca8fea83c0
1 changed files with 12 additions and 0 deletions
12
easy/number_complement.py
Normal file
12
easy/number_complement.py
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
# 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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue