[LeetCode By Go 70]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?

解题思路

如果n是3的x次方,那么以3为低对数后一定是一个整数,否则不是
参考了EbowTang的博客http://blog.csdn.net/ebowtang/article/details/50485622

代码

func isPowerOfThree(n int) bool {
    if 0 >= n {
        return false
    }
    res := math.Log10(float64(n)) / math.Log10(float64(3))
    
    var res1 float64
    res1 = res - float64(int(res))
    fmt.Printf("res:%+v, res1:%+v, int res:%+v\n", res, res1, int(res))
    if res1 < 1E-14 {
        return true
    }
    
    return false
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容