class Solution:
def findComplement(self, num: int) -> int:
if num == 0:
return 1
mask = 0
temp = num
while temp:
mask = (mask << 1) | 1
temp >>= 1
return num ^ mask
class Solution {
public:
int findComplement(int num) {
if (num == 0) return 1;
int mask = 0, temp = num;
while (temp) {
mask = (mask << 1) | 1;
temp >>= 1;
}
return num ^ mask;
}
};
class Solution {
public int findComplement(int num) {
if (num == 0) return 1;
int mask = 0, temp = num;
while (temp != 0) {
mask = (mask << 1) | 1;
temp >>= 1;
}
return num ^ mask;
}
}
var findComplement = function(num) {
if (num === 0) return 1;
let mask = 0, temp = num;
while (temp) {
mask = (mask << 1) | 1;
temp >>= 1;
}
return num ^ mask;
};
The Number Complement problem asks you to find the complement of a given positive integer num
.
The complement of a number is defined as flipping all the bits in its binary representation (i.e., changing all 0s to 1s and all 1s to 0s), but only for the bits that are present in the original number (ignoring leading zeros).
For example, if num = 5
(which is 101
in binary), its complement is 010
in binary, which is 2
in decimal.
num
(guaranteed to be non-negative).num
as an integer.num
(no leading zeros).At first glance, you might think of converting the number to binary, flipping each bit, and then converting it back to decimal. While this works, it can be inefficient, especially if you use string manipulation.
A more optimized approach is to work directly with bits. The key observation is that you only need to flip the bits that are present in the number (that is, up to the most significant 1 in the binary representation). Any bits beyond that are just leading zeros and should not be flipped.
The challenge is to create a "mask" that has 1s in all the positions where num
has bits, and 0s elsewhere. Then, using the XOR operation, you can flip just those bits.
num
is 0, its complement should be 1 (since 0 in binary is 0, and flipping gives 1).mask
to 0 and temp
to num
.temp
is not zero, shift mask
left by 1 and set the least significant bit to 1 (i.e., mask = (mask << 1) | 1
).temp
right by 1 each time to count the number of bits in num
.mask
will have all 1s in the positions where num
has bits.num ^ mask
. This will flip all the bits in num
up to its most significant 1.This approach avoids converting to strings and leverages bitwise operations for efficiency.
Let's walk through the process with num = 5
:
num = 5
in binary is 101
.111
in binary (decimal 7).num
with mask
:
num
.The Number Complement problem is elegantly solved with bitwise operations. By constructing a mask of 1s that matches the length of the original number's binary representation, and then XORing the number with this mask, you efficiently flip only the relevant bits. This avoids unnecessary string manipulation and leverages the power of bitwise math, making the solution both fast and concise.