LeetCode 476 Number Complement

Code

Solution of problem 476 is showed below. You can also find it here.

My solution

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int findComplement(int num) {
int ret = 0, cnt = 0;
while(num) {
ret |= ((num & 1) ^ 1) << cnt;
num >>= 1;
++cnt;
}
return ret;
}
};

A solution on Leetcode:

1
2
3
4
5
6
7
8
9
class Solution {
public:
int findComplement(int num) {
int mask = 1;
while(mask < num)
mask = (mask << 1) + 1;
return num ^ mask;
}
};

Solution

This is a simple and easy problem. Just flip each bit and it’s all solved. You can iterate each bit or handle them all together.

Valueable Method

When seeing this kind of problem, I used to handle each bit and assemble them together. However, vectorize them (create a number and XOR with the original number) is a better way. Also, I ignore the feature that the all 1 bits sequence is the largest number at the sequence length.

Think more and code less all the time.