2017/11/5 Leetcode 日记
476. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
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.
Solutions:
class Solution { public: int findComplement(int num) { unsigned mask = ~0; while(mask & num) mask <<=1; return ~mask & ~num; } };