Given an integer, write a function to determine if it is a power of two.
Credits:
public class Solution {
public boolean isPowerOfTwo(int n) {
if (n <= 0) {
return false;
}
boolean isMeetOne = false;
for (int i = 0; i < 32; i++) {
if ( (n & 1) == 1 ) {
if (isMeetOne) {
return false;
}
isMeetOne = true;
}
n = n >> 1;
}
return true;
}
}
========
public class Solution {
public boolean isPowerOfTwo(int n) {
if (n <= 0) {
return false;
}
return (n & (n - 1)) == 0;
}
}
========
public class Solution {
public boolean isPowerOfTwo(int n) {
if (n <= 0) {
return false;
}
return (n & (n - 1)) == 0;
}
}
没有评论:
发表评论