给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂。
示例:
当 num = 16 时 ,返回 true 。 当 num = 5时,返回 false。
问题进阶:你能不使用循环/递归来解决这个问题吗?

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

C++:

方法一:

class Solution {
public:
    bool isPowerOfFour(int num) {
        while(num&&(num%4==0))
        {
            num/=4;
        }
        return num==1;
    }
};

 方法二:

class Solution {
public:
    bool isPowerOfFour(int num) {
        return num>0&&!(num&num-1)&&(num-1)%3==0;
    }
};

 参考:https://www.cnblogs.com/grandyang/p/5403783.html

相关文章:

  • 2022-01-24
  • 2021-05-02
  • 2022-12-23
  • 2022-12-23
  • 2021-07-20
  • 2021-10-18
  • 2021-05-29
  • 2021-07-30
猜你喜欢
  • 2021-04-10
  • 2021-04-15
  • 2021-04-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案