首页 > 其他 > 详细

LeetCode-326. Power of Three

时间:2016-02-18 19:29:25      阅读:141      评论:0      收藏:0      [点我收藏+]

Description:

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

判断一个整数是否是3的幂。

基于公式log3^n可以得出y=lg(n)/log(3)如果y正好是个整数的话,说明n是3的幂。也可以使用循环除3的方法,也不是很浪费时间循环不了多少次。

public class Solution {
    public boolean isPowerOfThree(int n) {
        
        double ans = Math.log(n) / Math.log(3);
        return Math.abs(ans - Math.round(ans)) < 0.000000000001;
    }
}

技术分享

LeetCode-326. Power of Three

原文:http://www.cnblogs.com/wxisme/p/5199070.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!