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) {
double x=Math.log(num)/Math.log(4);
int y=(int)x;
return(x==y);
}
}
本文提供了一个函数,用于判断给定的32位整数是否为4的幂次。通过数学运算实现,避免了循环和递归。
749

被折叠的 条评论
为什么被折叠?



