[leetcode]342. 4的幂

1、题目描述:
  给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方。

  示例 1:

  输入: 16
  输出: true

  示例 2:

  输入: 5
  输出: false

  进阶:
  你能不使用循环或者递归来完成本题吗?

2、思路:
  参考题解
3、代码:
  
class Solution {
    public boolean isPowerOfFour(int num) {
        if((num & 0xaaaaaaaa)==0 &&Integer.bitCount(num)==1) return true;
        else return false;
    }
}
View Code
4、考察重点:位运算
5、参考资料:
   Integer.bitCount(int i)返回指定 int 值的二进制补码表示形式的 1 位的数量
6、参考链接:
  Java API
7、题目链接:
  https://leetcode-cn.com/problems/power-of-four/

猜你喜欢

转载自www.cnblogs.com/DoubleBarbecue/p/11363794.html