您的位置:首页 > 其它

231. Power of Two

2016-03-18 21:01 399 查看
Given an integer, write a function to determine if it is a power of two.

就是看n是不是2的幂,n&(n-1)可以找出n中二进制下有多少1
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n<=0)return false;
return ((n&(n-1))==0);//n&(n-1)用在判断二进制数中有多少个1存在非常好用
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: