0
Leetcode Problem 1009. Complement of Base 10 Integer
1009. Complement of Base 10 Integer
AI Mock Interview
Leetcode Solutions
Compute Bit Length and Construct-bit Bitmask
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
If the input number is 0, return 1 as its complement is 1.
Calculate the bit length
l
of the input number
n
(number of bits required to represent
n
).
Construct a bitmask with
l
bits all set to 1. This can be done by shifting 1 to the left by
l
bits and subtracting 1.
Perform an XOR operation between the input number
n
and the bitmask to get the complement.
Return the result of the XOR operation as the complement of
n
.
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...