342. Power of Four (判断一个数是否为4的幂)

谁践踏了优雅 2022-07-15 01:36 253阅读 0赞

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example:
Given num = 16, return true. Given num = 5, return false.

Follow up: Could you solve it without loops/recursion?

  1. public class Solution {
  2. public boolean isPowerOfFour(int num) {
  3. return num>0&&Integer.bitCount(num)==1&&(num&0x55555555)!=0;
  4. }
  5. }

发表评论

表情:
评论列表 (有 0 条评论,253人围观)

还没有评论,来说两句吧...

相关阅读