LeetCode-326. Power of Three
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; } }

作者:Pickle
声明:对于转载分享我是没有意见的,出于对博客园社区和作者的尊重一定要保留原文地址哈。
致读者:坚持写博客不容易,写高质量博客更难,我也在不断的学习和进步,希望和所有同路人一道用技术来改变生活。觉得有点用就点个赞哈。








浙公网安备 33010602011771号