Leet Code OJ 326. Power of Three [Difficulty: Easy]

题目:
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的幂。
更进一步:你能不使用循环或者递归来完成吗?

代码:

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

你可能感兴趣的:(LeetCode,算法,幂)