Given an integer, write a function to determine if it is a power of two.

class Solution {
public:
    bool isPowerOfTwo(int n) {
        int num = 0;
        while (n > 0) {
            if (n &1) num++;
            n/=2;
        }
        if (num == 1) return true;
        return false;
    }
};

 

相关文章:

  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2021-05-02
  • 2021-06-23
  • 2021-10-16
  • 2022-02-01
  • 2021-09-19
猜你喜欢
  • 2021-05-22
  • 2022-03-05
  • 2021-07-30
  • 2021-08-01
  • 2022-12-23
相关资源
相似解决方案