4. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
Example 1:
Input: 5 Output: 2 Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
Example 2:
Input: 1 Output: 0 Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
Note:
- The given integer is guaranteed to fit within the range of a 32-bit signed integer.
- You could assume no leading zero bit in the integer’s binary representation.
class Solution {
public int findComplement(int num) {
return num ^ ((1<<Integer.toString(num, 2).length()) - 1);
}
}
A thoughtful note is a perfect way to express your gratitude. Makes the feeling very unique by directly telling them how they have brightened up your day. Don't think about having some fun or admitting how happy you are to use their gift! Begin with one of these sentiments in your letter:
I just wanted to write to say thank you for being here! Thank you for the pretty visit, I once pray that I may come in somebody's help. And I wish you will come every time after every visit.
😍🎈✨🎉