(LeetCode 342) Power of Four

来源:互联网 发布:淘宝怎么做活动 编辑:程序博客网 时间:2024/04/30 00:24

Q:
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?

solution:
解法很多,思路和之前的方法一样:
(LeetCode 326)Power of Three

(LeetCode 231)Power of Two

4x=n
x=log(n)/log(4)
判断x是否为整数

class Solution {public:    bool isPowerOfFour(int num) {        double x = log10(num)/log10(4);        if(x==int(x))return true;        return false;    }};
1 0