leetcode-342-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?

分析


和那道2的n次幂,用log函数即可。

解决方案1(C++)


1
2
3
4
5
6
7
8
9
10
class Solution {
public:
bool isPowerOfFour(int num) {
if(num <= 0) {
return false;
}else {
return (log(num)/log(4)) == (int)(log(num)/log(4));
}
}
};

相关问题


(E) Power of Two
(E) Power of Three

题目来源