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.
Have you met this question in a real interview?
思路:不断除2,看是否能除尽就行
代码如下:
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n<1) return false;
if(n ==1) return true;
while(n>1){
if(n%2!=0) return false;
n = n/2;
}
return true;
}
}