Given an integer, write a function to determine if it is a power of two.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
代码如下:
public class Solution { public boolean isPowerOfTwo(int n) { if(n<=0) return false; if(n==1) return true; int mod=n; while(mod>=2 && mod%2==0){ mod/=2; } if(mod>=2 && mod%2!=0) return false; else return true; } }
运行结果: