1.eamples

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.
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.
Subscribe to see which companies asked this question.

 

2.solve

class Solution {
public:
    int findComplement(int num) {
        int mask=1;
        int temp = num;
        while(num)
         {
             num>>=1;
             mask<<=1;
         }
        mask--;
        return (temp^mask); 
    }
};

 

  

相关文章:

  • 2021-06-02
  • 2021-11-20
  • 2021-05-19
  • 2022-02-15
猜你喜欢
  • 2021-05-17
  • 2022-12-23
  • 2022-12-23
  • 2021-08-23
  • 2022-01-31
  • 2021-10-26
相关资源
相似解决方案