给定一个整数,写一个函数来判断它是否是2的幂。

详见:https://leetcode.com/problems/power-of-two/description/

Java实现:

class Solution {
    public boolean isPowerOfTwo(int n) {
        return (n>0)&&((n&(n-1))==0);
    }
}

 C++实现:

class Solution {
public:
    bool isPowerOfTwo(int n) {
        return (n>0)&&(!(n&(n-1)));
    }
};

  

相关文章:

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