IncredibleThings

导航

LeetCode-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?
public class Solution {
    private static final double epsilon=10e-15;
    public boolean isPowerOfThree(int n) {
        
        if(n<=0){return false;}
        if(n==1){return true;}
        double res=Math.log(n)/Math.log(3);
        return Math.abs(res - Math.round(res)) < epsilon;
    }
}

 

posted on 2016-07-19 03:05  IncredibleThings  阅读(126)  评论(0编辑  收藏  举报