Complement of Base 10 Integer LeetCode solution in Python

1
class Solution:
2
def bitwiseComplement(self, n: int) -> int:
3
cnt = 0
4
ans = 0
5
if n == 0:
6
return 1
7
while n > 0:
8
if n & 1:
9
cnt += 1
10
else:
11
ans = ans + (2**cnt)
12
cnt += 1
13
n = n >> 1
14
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0