0
Leetcode Problem 476. Number Complement
476. Number Complement
AI Mock Interview
Leetcode Solutions
Compute Bit Length and Construct-bits Bitmask
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Calculate the bit length
l
of the input number
num
by using the formula
l = floor(log2(num)) + 1
.
Construct the bitmask by shifting 1 to the left by
l
bits and subtracting 1, which gives a sequence of
l
1-bits.
Perform an XOR operation between
num
and the bitmask to get the complement.
Return the result of the XOR operation as the complement of
num
.
Flip Bit by Bit
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...