My Java solution


  • 0
    T
    public class Solution {
        public boolean isPowerOfThree(int n) {
            if (n == 0) return false;
            if (n == 1) return true;
            while (n >= 3){
                if (n%3 == 0 && n!=3) {
                    n /= 3;
                } else{
                   n %= 3;
                }
            }
            return n==0? true:false;
        }
    }

  • 0
    M

    You can just use "return n ==0;" instead of "return n==0? true:false;"


Log in to reply
 

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