231. Power of Two - jiejackyzhang/leetcode-note GitHub Wiki
Given an integer, write a function to determine if it is a power of two.
##Approach 1
public class Solution {
public boolean isPowerOfTwo(int n) {
return n > 0 && Math.pow(2,30) % n == 0;
}
}
##Approach 2 注意到power of 2有以下特征:8(1000),8-1(0111),8 & (8-1) == 0000。
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n < 1) return false;
return ((n-1) & n) == 0;
}
}