[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
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容