Question
Link : https://leetcode.com/problems/power-of-four/
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?
Code
直接上一个非递归/非循环版本的,之前有个是判断一个数是不是3的幂,和这个是有异曲同工之妙的。(C++ : 9ms)
class Solution {
public:
bool isPowerOfFour(int num) {
if(num == 0)
return false;
return pow(4, int(log(num) / log(4))) == num;
}
};