C++ Solution with sqrt and bit tricks and without loop/recursion.


  • 1
    S
    class Solution {
    public:
        bool isPowerOfFour(int num) {
            if (num <= 0) return false;
            int root = round(sqrt(num));
            // check if num was a perfect square.
            if (num != root * root){
                return false;
            } 
            // check if root is a power of 2.
            return (root & (root - 1)) == 0;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.