Java recursive solution, AC


  • -1
    Y

    simple code as follows, return false when modulo fails

    public boolean isPowerOfThree(int n) {
        if (n <= 0) {
            return false;
        }
        while (n > 1) {
            if (n % 3 != 0) {
                return false;
            }
            n /= 3;
        }
        return true;
    }

  • 0
    E

    You're not supposed to use a while loop here as indicated by the description :)


Log in to reply
 

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