(easy)LeetCode 231.Power of Two

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;
}
}

  运行结果:

(easy)LeetCode  231.Power of Two

上一篇:iomanip


下一篇:安徽省2016“京胜杯”程序设计大赛_E_转啊转