1.题目:原题链接

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

给定一个整数,判断该整数是否是2的n次幂。

 

2.思路

如果一个整数是2的n次幂,那么首先其应当是正数,其次该数的二进制表示必定是以1开头,后续若有数字必为0.

 

3.代码

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

  

相关文章:

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