LeetCode By Go

[LeetCode By Go 71]231. Power of

2017-08-30  本文已影响5人  miltonsun

题目

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
}
上一篇下一篇

猜你喜欢

热点阅读