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:

  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. 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;
    }
};
c++

相关文章:

  • 2021-10-12
  • 2021-11-17
  • 2022-12-23
  • 2022-02-19
  • 2021-05-17
  • 2022-02-23
  • 2021-09-21
猜你喜欢
  • 2021-07-24
  • 2021-12-02
  • 2021-12-18
  • 2022-03-08
  • 2021-09-24
  • 2021-11-26
  • 2021-09-13
相关资源
相似解决方案