題目
Given an integer, write a function to determine if it is a power of two.
解題思路
任何一個(gè)2的x次方一定能被int型里最大的2的x次方整除,這里L(fēng)eetCode使用的應(yīng)該是64位服務(wù)器,因?yàn)閕nt最大值是262 = 4611686018427387904
注意
整數(shù)包含負(fù)數(shù)
不能使用求指數(shù)的方式,因?yàn)橛芯葐?wèn)題
參考了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
}