We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 11eeba4 commit c09a85dCopy full SHA for c09a85d
scripts/algorithms/C/Complement of Base 10 Integer/Complement of Base 10 Integer.py
@@ -1,14 +1,16 @@
1
-# Runtime: 50 ms (Top 42.69%) | Memory: 13.9 MB (Top 55.15%)
2
-class Solution(object):
3
- def bitwiseComplement(self, n):
4
- m = n
5
- mask = 0
+// Runtime: 38 ms (Top 52.6%) | Memory: 17.10 MB (Top 12.47%)
6
+class Solution:
+ def bitwiseComplement(self, n: int) -> int:
+ cnt=0
+ ans=0
7
if n==0:
8
return 1
9
- while m:
10
- mask = (mask << 1) | 1
11
- m = m>>1
12
-
13
- ans = mask & (~n)
+ while n>0:
+ if n&1:
+ cnt+=1
+ else:
+ ans =ans +(2**cnt)
14
15
+ n=n>>1
16
return ans
0 commit comments