
题目描述
1 |
Given an integer, write a function to determine if it is a power of two. |
解题思路
- n=n/2,算n%2获取的商是否为1,1的话返回true,其他返回false
- 判断其是否是2的幂,例如8对应的二进制表示为:1000,7对应的二进制表示是0111,8&7==0 所以8是2的幂
Go实现1
1 |
func isPowerOfTwo(n int) bool { |
Go实现2
1 |
func isPowerOfTwo(n int) bool { |




近期评论