LeetCode - Power of Four

1076 ワード

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;
    }
};