C++ Solution without loop/recursion


  • 0
    A

    Simple idea coming from similar quesions: Power of Two/Three
    Add one additional restriction so that num is power of 4 rather than power of 2:

    (int)(log(num)/log(2))%2 == 0
    

    Solution:

    class Solution {
    public:
        bool isPowerOfFour(int num) {
            if (num <= 0)
                return false;
            int t = pow(4, (int)(log(INT_MAX)/log(4)));
            return (t%num == 0 && (int)(log(num)/log(2))%2 == 0);
        }
    };
    

  • 0
    Y

    could you explain "(int)(log(num)/log(2))%2 == 0"?


  • 0
    A
    This post is deleted!

  • 0
    A

    @yangjiannr
    Hi, just as I said "power of 4 rather than power of 2".

    For numbers that are power of 4, the division result would be even, but for those that are only power of 2, this would be odd.

    Find some numbers and try for yourself :-D


Log in to reply
 

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