Number Complement (LeetCode 476): Solution and Explanation
Flip every bit of num from its highest 1 down to bit 0 and return the resulting integer.
Intuition
A plain bitwise NOT flips all 32 bits, including the leading zeros above num's highest 1. Those turn into 1s and the result becomes a large negative number, which is not what the problem wants.
So the flip has to be limited to the bits num actually uses. A mask with 1s in exactly those positions does that: XOR with 1 flips a bit and XOR with 0 leaves it alone.
The mask is easy to grow. Start at 1 and keep shifting left and adding a 1 (1, 11, 111, ...) until the mask is at least num. At that point it is exactly as long as num.
Approach: All-ones mask plus XOR
- Set
mask = 1. - While
mask < num, setmask = (mask << 1) | 1. - Return
num ^ mask.
Step-by-step example
num = 5
- 5 is 101 in binary, 3 bits long.
- mask = 1 (1). 1 < 5, so grow to 11 (3).
- 3 < 5, so grow to 111 (7).
- 7 >= 5, so stop. The mask covers all 3 bits.
- 101 ^ 111 = 010 = 2. Return 2.
Code: Python, Java, C++
1class Solution:
2 def findComplement(self, num: int) -> int:
3 mask = 1
4 while mask < num:
5 mask = (mask << 1) | 1
6 return num ^ mask1class Solution {
2 public int findComplement(int num) {
3 int mask = 1;
4 while (mask < num) {
5 mask = (mask << 1) | 1;
6 }
7 return num ^ mask;
8 }
9}1class Solution {
2public:
3 int findComplement(int num) {
4 int mask = 1;
5 while (mask < num) {
6 mask = (mask << 1) | 1;
7 }
8 return num ^ mask;
9 }
10};Time and space complexity
O(log num)
The mask gains one bit per iteration, and num has about log2(num) bits.
O(1)
Only the mask is stored.
Other approaches
Bit length formula (time O(1), space O(1))
Compute the bit length L directly (num.bit_length() in Python, 32 - Integer.numberOfLeadingZeros(num) in Java) and return num ^ ((1 << L) - 1). Same idea, no loop.
Flip bit by bit (time O(log num), space O(1))
Walk the bits of num from low to high, and for each one add (1 - bit) << i to the answer. Easy to follow, a bit more code.
Common mistakes
- Returning ~num. It flips the leading zeros too and gives a negative number.
- Building the mask as (1 << L) - 1 with L = 31 is fine, but L = 32 overflows a 32-bit int. With num < 2^31 the length is at most 31, and the loop version never shifts past 31 bits.
- Stopping the loop at mask <= num instead of mask < num. When num is all ones (like 7) the mask equals num and must stop there, otherwise it grows one bit too long and the answer gains an extra 1.
Frequently asked questions
Why does XOR with the mask flip the bits?
b ^ 1 is the opposite of b, and b ^ 0 is b. The mask has 1s exactly under num's significant bits and 0s above, so those bits flip and the leading zeros stay 0.
Why is mask >= num the right stopping point?
An all-ones mask of length L equals 2^L - 1, the largest L-bit number. The first such mask that is at least num is the one with the same length as num.
Is this the same as LeetCode 1009, Complement of Base 10 Integer?
Yes, except 1009 allows n = 0, whose complement is 1. Starting the mask at 1 already handles that case, since 0 ^ 1 = 1.