美文网首页LeetCode By Go
[LeetCode By Go 71]231. Power of

[LeetCode By Go 71]231. Power of

作者: miltonsun | 来源:发表于2017-08-30 11:53 被阅读5次

题目

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

解题思路

任何一个2的x次方一定能被int型里最大的2的x次方整除,这里LeetCode使用的应该是64位服务器,因为int最大值是262 = 4611686018427387904
注意
整数包含负数
不能使用求指数的方式,因为有精度问题

参考了EbowTang的博客http://blog.csdn.net/ebowtang/article/details/50485622

代码

func isPowerOfTwo(n int) bool {
    if 0 >= n{
        return false
    }
    
    res := 4294967296 % n
 
    if 0 == res  {
        return true
    }
    
    return false
}

相关文章

网友评论

    本文标题:[LeetCode By Go 71]231. Power of

    本文链接:https://www.haomeiwen.com/subject/rrnzdxtx.html