342. Power of Four (判断一个数是否为4的幂)
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?
public class Solution {
public boolean isPowerOfFour(int num) {
return num>0&&Integer.bitCount(num)==1&&(num&0x55555555)!=0;
}
}
还没有评论,来说两句吧...