Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 39d1bfb

Browse files
Time: 41 ms (14.09%), Space: 16.4 MB (58.08%) - LeetHub
1 parent 04201e3 commit 39d1bfb

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def findComplement(self, num: int) -> int:
3+
# Calculate the bit length of num
4+
bit_length = num.bit_length()
5+
6+
# Create a bitmask with all bits set to 1, having the same length as num's binary representation
7+
bitmask = (1 << bit_length) - 1
8+
9+
# XOR num with the bitmask to get the complement
10+
return num ^ bitmask

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /