326. Power of Three

Given an integer, write a function to determine if it is a power of three.

Follow up:

Could you do it without using any loop / recursion?


问题:判断是否是3的幂?

思路:递归

java代码:

public class Solution {
    public boolean isPowerOfThree(int n) {
        //break condition
        if(n==0) return false;
        
        //break condition
        else if(n==1) return true;
        
        //Decreasing condition
        else if (n%3==0)
            return isPowerOfThree(n/3);
            
        else
            return false;
    }
}




你可能感兴趣的:(LeetCode,power)